2012-04-13 26 views
2

我需要檢查一個字符串是否包含軟式炒作,以及它是否將其從字符串中移除。有關在將字符串保存到數據庫之前找到它並將其刪除的最佳方法的建議?PHP:檢查字符串是否包含軟式炒作並將其替換

謝謝!

+0

您是否檢查過PHP文檔? http://us.php.net/manual/en/book.strings.php – Madbreaks 2012-04-13 21:00:24

回答

9

軟連字符是無形這裏,但這將其刪除:

$str = str_replace('­', '', $str); 
+0

工作。謝謝! – anthony 2012-04-13 21:05:07

+0

@anthony沒問題:)你可能想要很好地評論這條線,或者使用hakre的答案來使代碼更加清晰。 – Paulpro 2012-04-13 21:07:52

4

試試這個:

$str = str_replace('-','',$str); 
+0

這適用於普通的連字符,但它沒有找到在這裏提到的軟式炒作:http://en.wikipedia.org/wiki/Soft_hyphen。 – anthony 2012-04-13 21:00:48

5

您鏈接軟連字符(如果你的字符串是ISO 8859 -1):

$str = strtr($str, array("\xAD" => "")); 

否則你需要找到它的字節序列i n您使用的編碼,一些值SOFT HYPHEN (U+00AD)常見的字符集給出:

\xAD  ISO-8859-1; ISO-8859-2; ISO-8859-3; ISO-8859-4; ISO-8859-5; 
      ISO-8859-6; ISO-8859-8; ISO-8859-9; ISO-8859-15; Windows 1250; 
      Windows 1251; Windows 1252; Windows 1253; Windows 1254; Windows 
      1255; Windows 1256; Windows 1257; Windows 1258 

\xF0  OEM 775; OEM 850; OEM 852; OEM 855; OEM 857; OEM 858; 

\xC2\xAD UTF-8 
2

的答案都不在這裏爲我工作。我能夠刪除軟連字符的唯一方法是:

preg_replace('~\x{00AD}~u', '', $str); 
相關問題