2014-10-21 46 views
0

如何用字符串替換特殊字符½與.5中的字符串7½。我想輸出字符串爲7.5如何在php中替換½與.5

preg_replace('/\x{EF}\x{BF}\x{BD}/u', '.5', iconv(mb_detect_encoding($str), 'UTF-8', $str)); 

沒有更換爲0.5

+0

你檢查過正則表達式是否匹配嗎?例如'preg_match('/ x ..... /''如果正則表達式應該返回TRUE。 – 2014-10-21 21:34:42

回答

1

你嘗試過:

$str = "how to replace special character ½ with .5 in a string 7½. i want to output string as 7.5"; 
echo preg_replace('/½/u', '.5', iconv(mb_detect_encoding($str), 'UTF-8', $str)); 
//or 
echo preg_replace('/½/u', '.5', $str); 
//or 
echo preg_replace('/½/', '.5', $str); 
+0

沒有一個能解決這個問題。 – ashish1dev 2014-10-22 06:16:58

+0

@ user1812318:所有這三種解決方案對我來說都很好, – Toto 2014-10-22 07:01:31

+0

可以。您的編輯器需要更改其設置?您使用的是什麼? – user1269942 2014-10-22 16:29:51

0

str_replace函數更快,比preg_replace函數,並且也做的伎倆

$str = "how to replace special character ½ with .5 in a string 7½. i want to output string as 7.5"; 

echo str_replace('½', '.5', $str); 
+0

str_replace函數僅適用於UTF-8字符。½是非-ytf編碼字符。 – ashish1dev 2014-10-22 06:17:45