我想从字符串中删除零宽度非连接符字符,但使用str_replace
没有用。
str_replace可以解决这个问题,只要你注意替换的内容。
// \xE2\x80\x8C is ZERO WIDTH NON-JOINER
$foo = "foo\xE2\x80\x8Cbar";
print($foo . " - " . strlen($foo) . "\n");
$foo = str_replace("\xE2\x80\x8C", "", $foo);
print($foo . " - " . strlen($foo) . "\n");
输出结果如预期:
foobar - 9
foobar - 6
\xE2\x80\x8C
)它的名称是什么? - Mohammad Kermanistr_replace可以实现你想要的功能,但是PHP对Unicode的本地支持不是很好。以下代码可以实现你的需求。由于PHP不支持\u语法,因此使用了json_decode来获取Unicode字符。
<?php
$unicodeChar = json_decode('"\u200c"');
$string = 'blah'.$unicodeChar.'blah';
echo str_replace($unicodeChar, '', $string);
?>
编辑:虽然我的方法可行,但我建议您使用fiskfisk的解决方案。它比使用json_decode更加专业。
"\u{200C}"
。 - IMSoP