php 替换   隐藏在文本中的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9870974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:49:54  来源:igfitidea点击:

replace   characters that are hidden in text

phpregexunicode

提问by Behnam

How to remove  (that are hidden) and SPACES in below text but

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

  • hold UNICODE characters
  • hold <br>tag
  • 保存 UNICODE 字符
  • 持有<br>标签

i tested:

我测试:

  • i used trim($string)=> NOT WORKED
  • i used str_replace('&nbsp;', '', $string)=> NOT WORKED
  • i used some regex => NOT WORKED

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

                <br>????? ????: ?????? ?? ???? ??? ????
    

UPDATE: Image of hidden  Thanks

更新:Image of hidden  谢谢

回答by Mostafa M

This solution will work, I tested it:

此解决方案将起作用,我对其进行了测试:

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

回答by Ben Carey

Not tested, but if you use something like:

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

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

That should remove all spaces.

那应该删除所有空格。

UPDATE

更新

To remove all spaces and &nbsp;references, use something like:

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

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

UPDATE 2

更新 2

Try this:

尝试这个:

$string = html_entity_decode($string);

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

echo $string;

Forgot to say, reconvert the html entities so add this after the replacement:

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

htmlentities($string);

回答by Oleksiy Muzalyev

All solutions above kind of work, until one starts to work with German language where there are such letters:

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

? &auml;

and othere simial ones. I use the following code:

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

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

More details here: PCRE(3) Library Functions Manual

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

回答by FullStackEngineer

This worked for me.

这对我有用。

preg_replace("/&nbsp;/",'',$string)