我正在读入并解析一个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命令得到以下结果:
% 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
发布于 2020-03-20 21:41:38
当您使用/u (PCRE_UTF8 $linein )时,无效的主题修饰符将不匹配任何内容。要解决这个问题,请确保您传递的字符串是UTF-8。
如果您的字符串是使用ISO-8859-1编码的,请尝试将其转换为UTF8:
$cleaned_line = preg_replace( "/[^$match_list]/u", "*", utf8_encode($linein) );否则,请查看mb_convert_encoding()函数。
https://stackoverflow.com/questions/44947062
复制相似问题