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

Iva*_*van 2 php utf-8 html-entities

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

This is a string

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

<p style="width:50px">This&nbsp;is&nbsp;a&nbsp;string</p>
Run Code Online (Sandbox Code Playgroud)

为了获得&nbsp;实体,我尝试使用以下过滤器但没有成功:

$new = preg_replace("/&nbsp;/i", " ", $str);
$new = str_replace('&nbsp;', ' ', $str);
$new = html_entity_decode($str);
Run Code Online (Sandbox Code Playgroud)

你有一个PHP小提琴来看到这个实际操作(我必须从数据库输出中以十六进制编码字符串;字符串是西班牙语,对不起).

怎么处理这个?为什么html_entity_decode()不工作?那么替换功能呢?谢谢.

uni*_*iah 12

这变得棘手,它不像替换普通字符串那样直截了当.

试试这个.

 str_replace("\xc2\xa0",' ',$str); 
Run Code Online (Sandbox Code Playgroud)

或者,以上应该有效:

$nbsp = html_entity_decode("&nbsp;");
$s = html_entity_decode("[&nbsp;]");
$s = str_replace($nbsp, " ", $s);
echo $s;
Run Code Online (Sandbox Code Playgroud)

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

  • 只有这一行对我有用:`$s = str_replace($nbsp, "", $s);` (3认同)