PHP中的怪异破折号 [英] Weird dash character in PHP
问题描述
我的文字中有一个奇怪的破折号,在str_replace
中未检测到.
I have a weird dash in my text, which isn't being detected in a str_replace
.
这里是一个例子:
Sun: 10:00 – 3:00pm
我不知道破折号是否会显示在这里..但是当它插入到我的表中时,它就像一个带有字符0096
I don't know if the dash will show up on here.. but when it is inserted into my table, it is like a square box with the characters 0096
它看起来很像–
-在查看源代码时,没有特殊字符,只有破折号.
It looks a lot like an –
- when viewing the source, there is no special characters, just the dash.
str_replace('–', '', $var);
上面的替换似乎没有抓住,其他人以前有过这个麻烦吗?
The above replace doesn't seem to catch it, has anyone else had this trouble before?
推荐答案
这是 html_entity_decode
:
$endash = html_entity_decode('–', ENT_COMPAT, 'UTF-8');
echo str_replace($endash, '(en dash)', 'Sun: 10:00 – 3:00pm');
请注意,仅当您的网站编码为UTF-8 并且您的编辑器编码(或str_replace
的第三个参数的编码)也是如此时,此方法才有效.如果您使用其他编码(并且网站和编辑器都应使用相同的编码),请将html_entity_decode的第三个参数替换为其名称.
Note that this only works if your website encoding is UTF-8 and your editor encoding(or the encoding of the third argument to str_replace
) is as well. If you use another encoding (and you should use the same both for website and editor), replace the third parameter of html_entity_decode with its name.
这篇关于PHP中的怪异破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!