如何删除& nbsp;从UTF-8字符串? [英] How to remove   from a UTF-8 string?
问题描述
我的数据库返回一些字符串,如:
My database is returning some strings like:
此为& nbsp; a& nbsp;字符串
当字符串足够长并且您设置了最大宽度时,这是一个问题:
This is a problem when the string is long enough and you have maximum width set:
<p style="width:50px">This is a string</p>
为了获取& nbsp;
实体,我尝试使用以下过滤器但未成功:
In order to get ride of
entities I've tried to use the following filters without success:
$new = preg_replace("/ /i", " ", $str);
$new = str_replace(' ', ' ', $str);
$new = html_entity_decode($str);
您有一个 PHP提琴才能看到它的作用(我不得不从数据库输出中以十六进制形式对字符串进行编码;该字符串是西班牙文,抱歉).
You have a PHP fiddle to see this in action (I've had to codify the string in hex from the database output; the string is in spanish, sorry).
如何处理?为什么 html_entity_decode()
不起作用?那替换功能呢?谢谢.
How to deal with this? Why html_entity_decode()
is not working? And what about the replace functions? Thanks.
推荐答案
这很棘手,它不如替换普通字符串那样简单.
This gets tricky, its not as straight forward as replacing normal string.
尝试一下.
str_replace("\xc2\xa0",' ',$str);
或者这,上面的方法应该起作用:
or this, the above should work:
$nbsp = html_entity_decode(" ");
$s = html_entity_decode("[ ]");
$s = str_replace($nbsp, " ", $s);
echo $s;
@ref: https://moovwebconfluence.atlassian.net/wiki/pages/viewpage.action?pageId=1081435
这篇关于如何删除& nbsp;从UTF-8字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!