dongsheng1238 2015-02-24 19:29
浏览 285
已采纳

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

My database is returning some strings like:

This is a string

This is a problem when the string is long enough and you have maximum width set:

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

In order to get ride of &nbsp; entities I've tried to use the following filters without success:

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

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).

How to deal with this? Why html_entity_decode() is not working? And what about the replace functions? Thanks.

  • 写回答

2条回答 默认 最新

  • doujingdai5521 2015-02-24 19:40
    关注

    This gets tricky, its not as straight forward as replacing normal string.

    Try this.

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

    or this, the above should work:

    $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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置