如何在php中替换½替换½与.5

时间:2014-10-21 21:30:50

标签: php utf-8 preg-replace str-replace

如何在字符串7½中用.5替换特殊字符½。我想输出字符串为7.5

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

不会替换为.5

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);