替换文本中隐藏的字符

问题描述 投票:0回答:4

如何删除下面文本中的

 
(隐藏的)和空格但是

  • 保留 UNICODE 字符
  • 按住
    <br>
    标签

我测试过:

  • 我用过
    trim($string)
    => 没用过
  • 我用过
    str_replace('&nbsp;', '', $string)
    => 没用过
  • 我使用了一些正则表达式=>不起作用

                <br>تاريخ ورود: یکشنبه ۲۳ بهمن ماه ۱۳۹۰
    

更新:Image of hidden   谢谢

php regex unicode
4个回答
92
投票

这个解决方案可行,我测试过:

$string = htmlentities($content, null, 'utf-8');
$content = str_replace("&nbsp;", "", $string);
$content = html_entity_decode($content);

39
投票

未经测试,但如果您使用类似的东西:

$string = preg_replace("/\s/",'',$string);

这应该删除所有空格。

要删除所有空格和

&nbsp;
引用,请使用类似以下内容:

$string = preg_replace("/\s|&nbsp;/",'',$string);

你也可以尝试这个:

$string = html_entity_decode($string);

$string = preg_replace("/\s/",'',$string);

echo $string;

并通过在替换后添加以下内容来重新转换 html 实体:

htmlentities($string);

5
投票

上述所有解决方案都有效,直到开始使用德语,其中有这样的字母:

ä &auml;

以及其他类似的。 我使用以下代码:

$string = preg_replace ( "!\s++!u", ' ', $string );

更多详细信息请参见:PCRE(3) 库函数手册


3
投票

这对我有用。

preg_replace("/&nbsp;/",'',$string)
© www.soinside.com 2019 - 2024. All rights reserved.