2012-11-20 30 views

回答

5

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"); 

輸出:

foo‌bar - 9 
foobar - 6 
+0

你知道這類代碼的標題是什麼嗎? (我的意思是'\ xE2 \ x80 \ x8C')它的標題是什麼? – M98

+1

@Kermani通常只是[轉義序列](http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double)或轉義代碼;它們對於每種語言都可能是不同的,但大多數實現一個共同的子集(例如\ x,\ n,\ r等)。 – MatsLindh

0

str_replace函數會做你想要什麼,但PHP沒有對Unicode很好的本地支持。以下將做你的問題。 json_decode已被用於獲取Unicode字符,因爲PHP不支持\ u語法。

<?php 
$unicodeChar = json_decode('"\u200c"'); 
$string = 'blah'.$unicodeChar.'blah'; 
echo str_replace($unicodeChar, '', $string); 
?> 

編輯:雖然我的方法有效,但我建議您使用fiskfisk的解決方案。這比使用json_decode更簡單。