替换  隐藏在文本中的字符

Beh*_*nam 56 php regex

如何删除 (隐藏)和下面文本中的空格但是

  • 保留UNICODE字符
  • 持有<br>标签

我测试过:

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

                <br>????? ????: ?????? ?? ???? ??? ????
    
    Run Code Online (Sandbox Code Playgroud)

更新: 隐藏的图像   谢谢

最终解决方案:

            $string = htmlentities($string, null, 'utf-8');
            $string = str_replace("&nbsp;", "", $string);
Run Code Online (Sandbox Code Playgroud)

小智 58

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

$string = htmlentities($content, null, 'utf-8');
$content = str_replace("&nbsp;", "", $string);
$content = html_entity_decode($content);
Run Code Online (Sandbox Code Playgroud)

  • 我的 神。经过数小时的搜索,终于找到了解决方案!!非常感谢。我遇到了&nbsp;的问题 防止我的tinymce文本损坏得很好,所以我用一个真实的空格替换了它们:`function b09_remove_forced_spaces($ content){$ string = htmlentities($ content,null,'utf-8'); $ content = str_replace(“&nbsp;”,“”,$ string); $ content = html_entity_decode($ content); 返回$ content; } add_filter(“ the_content”,“ b09_remove_forced_spaces”,9);`(不好的事情是stackoverflow不允许注释中的代码块 (3认同)

Ben*_*rey 37

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

$string = preg_replace("/\s/",'',$string);
Run Code Online (Sandbox Code Playgroud)

这应该删除所有空格.

UPDATE

要删除所有空格和&nbsp;引用,请使用以下内容:

$string = preg_replace("/\s|&nbsp;/",'',$string);
Run Code Online (Sandbox Code Playgroud)

更新2

试试这个:

$string = html_entity_decode($string);

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

echo $string;
Run Code Online (Sandbox Code Playgroud)

忘了说,重新转换html实体,所以在替换后添加:

htmlentities($string);
Run Code Online (Sandbox Code Playgroud)

  • 与[@BalusC](http://stackoverflow.com/a/4515394/620410)不同的解决方案为我工作。 (2认同)

小智 5

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

\n\n
\xc3\xa4 &auml;\n
Run Code Online (Sandbox Code Playgroud)\n\n

和其他类似的。\n我使用以下代码:

\n\n
$string = preg_replace ( "!\\s++!u", \' \', $string );\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n