Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php preg_replace with empty string replacement character不适用于不可打印的字符_Php_Regex - Fatal编程技术网

php preg_replace with empty string replacement character不适用于不可打印的字符

php preg_replace with empty string replacement character不适用于不可打印的字符,php,regex,Php,Regex,我正在尝试删除字符串中不可打印的字符,除了我需要的一些字符 $arr = ['Ù', 'é', '€']; $string = "é & Ù @ ♣ ☂ % & € À"; $acceptedChars = implode('\\', $arr); $string = preg_replace('/[^[:print:] ' . $acceptedChars . ']/', '', $string); echo 'Test : ' . $string; 我

我正在尝试删除字符串中不可打印的字符,除了我需要的一些字符

$arr = ['Ù', 'é', '€'];
$string = "é & Ù @ ♣ ☂ % & € À";
$acceptedChars = implode('\\', $arr);
$string = preg_replace('/[^[:print:] ' . $acceptedChars . ']/', '', $string);

echo 'Test : ' . $string;
我的问题是,我得到的不是第二个参数中设置的空字符串来替换不需要的字符,而是:


删除除可打印ASCII字符和
$acceptedChars之外的所有字符
可以使用

$string=preg_replace('/[^-~'.$acceptedChars.]/u','.$string);

-~
模式是与任何可打印ASCII字符匹配的已知模式


u
修饰符是使正则表达式使用Unicode字符串所必需的。

您错过了
/u
修饰符
'/[^[:print:]['.$acceptedChars.]/u'
@WiktorStribiżew它似乎没有做我想要的事情在这种情况下,它只是使图标正确显示,但我想要的是不可打印的符号消失(由空字符串替换)你的意思是想
preg_替换('/[^-~'.$acceptedChars.]/u',''.$string)?删除除可打印ASCII字符和
$acceptedChars
之外的所有字符?@WiktorStribiżew这实际上解决了我的问题,谢谢!我发现有一些字符(我还没能准确地检测出是哪个)破坏了这个正则表达式的csv导出,而带有[:print:]的字符没有破坏,而是删除了所有带有重音符号(é、á、Û……)的字符。[:print:]删除的内容超过-~??我需要两者的组合,这样我可以删除破坏csv的字符,但允许我保留带有重音的字符(来自自定义字符数组)@naspy971我相信您有一个
^\-]/
。因此,您需要
$string=preg_replace('/[^-~'.preg_quote($acceptedChars,'/')。]/u','/$string)。或者,使用
[:print:][/code>,
preg_replace('/[^[:print:][/u',''$string)
解决我的问题,谢谢!但最后一个问题是^ \-]/包含什么样的符号?可能破坏csv导出的符号?@naspy971
^
\
-
]
是字符类中必须转义的符号,并且
/
必须转义,因为它用作正则表达式分隔符。