在PHP中使用preg_match的UTF正则表达式

I need a regeular expression for german words with ä,ü etc.

When I test this regex on this website https://regex101.com/

/^\p{L}+$/u

all is fine, but on my server I upload a CSV and want to parse the words. When I call with the word "Benedikt"

preg_match("/^[\p{L}]+$/u", $attributes[0])

I get false. The encoding of the CSV is UTF-8, when I convert it to ANSI, all is good but the ä,ü etc. is not shown correctly, so I think I should convert it to UTF-8. But why is it returning false?

The problem occurs because your csv file starts with a UTF-8 BOM. If you remove this, the regex works perfectly. I have confirmed it with this code:

<html>
<head>
<meta charset="utf-8" /> 
</head>
<body>
<?php
function remove_utf8_bom($text)
{
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    return $text;
}

$csvContents = remove_utf8_bom(file_get_contents('udfser_new.csv'));
$lines = str_getcsv($csvContents, "
"); //parse the rows

foreach ($lines as &$row) {
    $row = str_getcsv($row, ";");

    $firstName = $row[0];
    $lastName = $row[1];
    echo 'First name: ' . $firstName . ' - Matches regex: ' . (preg_match("/^[\p{L}]+$/u", $firstName) ? 'yes' : 'no') . '<br>';
    echo 'Last name: ' . $lastName . ' - Matches regex: ' . (preg_match("/^[\p{L}]+$/u", $lastName) ? 'yes' : 'no') . '<br>';
}
?>
</body>
</html>

The regex match the text successfully, and the ü in Glückmann is shown correctly on the page.

Result

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

http://php.net/manual/en/function.preg-match.php