我的数据库返回一些字符串,如:
This is a string
当字符串足够长并且设置了最大宽度时,这是一个问题:
<p style="width:50px">This is a string</p>
为了驾驭
实体,我尝试使用以下过滤器但未成功:
$new = preg_replace("/ /i", " ", $str);
$new = str_replace(' ', ' ', $str);
$new = html_entity_decode($str);
您有一个PHP fiddle to see this in action(我必须从数据库输出中将字符串以十六进制编码;该字符串是西班牙文,抱歉)。
如何处理?为什么html_entity_decode()
不起作用?那替换功能呢?谢谢。
这很棘手,它不如替换普通字符串那样直接。
尝试一下。
str_replace("\xc2\xa0",' ',$str);
或此,以上应该工作:
$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
让html实体替换您想要的实体并解码回去:
$str = str_replace(' ', ' ', htmlentities($new));
$new = html_entity_decode($str);
我认为strip_tags($string)
会为您完成这项工作。此功能用于删除html和php标签。