如何在php中替换替换½;5

如何在php中替换替换½;5,php,utf-8,preg-replace,str-replace,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您是否尝试过: $str = "how to replace special character ½ with .5 in a string 7½. i want to output string as 7.5"; echo pre

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

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

您是否尝试过:

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

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

你有没有检查正则表达式是否匹配?e、 g.
preg_match('/x…./'
如果正则表达式正确,则应返回TRUE。str_replace函数仅适用于UTF-8字符。½是非ytf编码字符。@user1812318:所有这三种解决方案对我都适用。您的编辑器是否需要更改其设置?您使用的是什么?