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


how to replace in php replace ½ with .5

如何将字符串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);