Php preg#u replace遇到ó后返回null;(急症室)

Php preg#u replace遇到ó后返回null;(急症室),php,regex,preg-replace,Php,Regex,Preg Replace,我正在读取并解析一个ANSI格式的CSV文件。在解析它之前,我想删除任何不在白名单中的字符 // remove any odd characters from string $match_list = "\x{20}-\x{5f}\x{61}-\x{7e}"; // basic ascii chars excluding backtick $match_list .= "\x{a1}-\x{ff}"; // extended latin 1 chars excluding control cha

我正在读取并解析一个ANSI格式的CSV文件。在解析它之前,我想删除任何不在白名单中的字符

// remove any odd characters from string
$match_list = "\x{20}-\x{5f}\x{61}-\x{7e}"; // basic ascii chars excluding backtick
$match_list .= "\x{a1}-\x{ff}"; // extended latin 1 chars excluding control chars
$match_list .= "\x{20ac}\x{201c}\x{201d}"; // euro symbol & left/right double quotation mark (from Word)
$match_list .= "\x{2018}\x{2019}"; // left/right single quotation mark (from word)

$cleaned_line = preg_replace("/[^$match_list]/u", "*",$linein); 
问题是,当它到达一个包含ó(急性o)字符的行时,它返回NULL。根据我的文本编辑器,这是xF3,所以应该是允许的

为什么它在preg_replace中抛出错误

更新-这似乎与文件有关-如果我将CSV文件中的问题行复制并粘贴到我的PHP文件中,就可以了

更新2-使用preg_last_error()我能够确定错误是:

 PREG_BAD_UTF8_ERROR    Returned by preg_last_error() if the last error was caused by malformed UTF-8 data (only when running a regex in UTF-8 mode).
我的文本编辑器刚刚将文件报告为ANSI,但使用unix文件命令,我得到以下结果:

% file PRICE_LIST_A.csv
PRICE_LIST_A.csv: Non-ISO extended-ASCII text, with CRLF line terminators

% file DOLLARS_PRICE_LIST.csv
DOLLARS_PRICE_LIST.csv: ISO-8859 text, with CRLF line terminators

% file PRICE_LIST_B.csv
PRICE_LIST_B.csv: Non-ISO extended-ASCII text, with CRLF line terminators

% file PRICE_LIST_TEST.csv
PRICE_LIST_TEST.csv: ASCII text, with CRLF line terminators

因此,我似乎从同一个会计应用程序中收到了各种编码的文件。我猜这些是无效的Unicode

当您使用
/u
(修饰符)时,无效的主题
$linein
将不匹配任何内容。要解决此问题,请确保传递的字符串是UTF-8

如果您的字符串使用ISO-8859-1编码,请尝试将其转换为UTF8:

$cleaned_line = preg_replace( "/[^$match_list]/u", "*", utf8_encode($linein) );

否则,请检查功能。

为什么要更换preg?str_replace不再适用于静态替换吗?似乎是。str_replace与模式不匹配?显示
var_dump($linein)的输出否则代码工作正常。@Wiktor Stribiżew谢谢,但它为我返回空值。Unix上的PHP5.6