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

标签 php utf-8 html-entities

我的数据库正在返回一些字符串,如:
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()不管用?那么替换功能呢?谢谢。

最佳答案

这变得棘手,它不像替换普通字符串那么简单。

尝试这个。

 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

关于php - 如何从 UTF-8 字符串中删除 ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28704562/

相关文章:

php 通过电子邮件从数据库中获取您的详细信息

PHP Soap 客户端 : How call WebService with Derived class as parameter?

php - json编码utf8错误

python - 在python中处理UTF-8数据

php - DOMDocument->saveHTML() 转换为空格

html - 如何在 Emacs 中使用 HTML 显示 XML 源代码?

ajax - 在 JSON 响应中编码 HTML 特殊字符的安全优势

php - MySQL - 如何在同一查询中使用 ROLLUP 和 ORDER BY

php - 通过 Javascript 或 PHP 删除 cookie

string - 使用 Perl 从字符串中删除 BOM