如何从UTF-8字符串中删除?

问题描述 投票:3回答:3

我的数据库返回一些字符串,如:

This is a string

当字符串足够长并且设置了最大宽度时,这是一个问题:

<p style="width:50px">This&nbsp;is&nbsp;a&nbsp;string</p>

为了驾驭&nbsp;实体,我尝试使用以下过滤器但未成功:

$new = preg_replace("/&nbsp;/i", " ", $str);
$new = str_replace('&nbsp;', ' ', $str);
$new = html_entity_decode($str);

您有一个PHP fiddle to see this in action(我必须从数据库输出中将字符串以十六进制编码;该字符串是西班牙文,抱歉)。

如何处理?为什么html_entity_decode()不起作用?那替换功能呢?谢谢。

php utf-8 html-entities
3个回答
13
投票

这很棘手,它不如替换普通字符串那样直接。

尝试一下。

 str_replace("\xc2\xa0",' ',$str); 

或此,以上应该工作:

$nbsp = html_entity_decode("&nbsp;");
$s = html_entity_decode("[&nbsp;]");
$s = str_replace($nbsp, " ", $s);
echo $s;

@ ref:https://moovwebconfluence.atlassian.net/wiki/pages/viewpage.action?pageId=1081435


0
投票

让html实体替换您想要的实体并解码回去:

$str = str_replace('&nbsp;', ' ', htmlentities($new));
$new = html_entity_decode($str);

-3
投票

我认为strip_tags($string)会为您完成这项工作。此功能用于删除html和php标签。

© www.soinside.com 2019 - 2024. All rights reserved.