【问题标题】:replace   characters that are hidden in text替换隐藏在文本中的字符
【发布时间】:2012-04-09 21:19:35
【问题描述】:

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

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

我测试过:

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

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

更新: 谢谢

【问题讨论】:

  • 在正则表达式中使用 \s 来匹配空格
  • 我做到了,但只是单词之间的空格发生了变化。  还存在
  • 对不起,你能把 html 发成文本让我复制一下吗
  • 整个页面上的任何内容都不适合我...
  • 上述问题的最终解决方案对我有用。即 $string = htmlentities($string, null, 'utf-8'); $string = str_replace(" ", "", $string);

标签: php regex unicode


【解决方案1】:

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

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

【讨论】:

  • O.我的。上帝。经过数小时的搜索,终于解决了!太感谢了。我遇到了   的问题防止我的 tinymce 文本很好地中断,所以我用一个真实的空间替换了所有它们:function b09_remove_forced_spaces($content) { $string = htmlentities($content, null, 'utf-8'); $content = str_replace("&amp;nbsp;", " ", $string); $content = html_entity_decode($content); return $content; } add_filter("the_content", "b09_remove_forced_spaces", 9);(stackoverflow 不允许 cmets 中的代码块的坏事
【解决方案2】:

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

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

这应该删除所有空格。

更新

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

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

更新 2

试试这个:

$string = html_entity_decode($string);

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

echo $string;

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

htmlentities($string);

【讨论】:

  • 好的,我看看能不能自己测试一下
  • 我无法测试它,但请尝试preg_replace("/(\s)|(\&amp;nbsp\;)/",'',$string);
  • 好的,我正在进一步调查
  • 你能把一些 html 贴到你的问题上,然后我就可以处理了
  • @BalusC 不同的解决方案对我有用。
【解决方案3】:

上述所有解决方案都有效,直到开始使用有此类字母的德语:

ä &auml;

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

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

更多详情:PCRE(3) Library Functions Manual

【讨论】:

    【解决方案4】:

    这对我有用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 2020-12-07
      相关资源
      最近更新 更多