【问题标题】:Replace html markup in strings [duplicate]替换字符串中的html标记[重复]
【发布时间】:2016-02-04 20:13:15
【问题描述】:

如何替换包含混合内容的字符串中的 html 元素,例如:

$str = "Find the   element in this string";

我想清除所有 &nbsp ;字符串中的元素。所以,我尝试了:

str_replace(" ", " ", $str);

我知道 &nbsp ;是空间并呈现为空白空间,但是我需要执行与我拥有的其他数据库操作相关的特定原因。

如果 str_replace 不适用于 HTML 标签,我应该使用正则表达式吗?

【问题讨论】:

标签: php html regex string str-replace


【解决方案1】:
$str = htmlentities($string, null, 'utf-8');
$str = str_replace(" ", "", $str);

【讨论】:

  • 谢谢,但它不起作用。我做到了: $string = "这是新的   sn-p"; $str1 = htmlentities($string, null, 'utf-8'); $str2 = str_replace(" ", "替换", $str1);回声 $str1;回声 $str2;
  • $str2 产生:这是新的   sn-p
  • 不能说为什么它不工作
  • 应该是  不是 &nbsp
猜你喜欢
  • 1970-01-01
  • 2014-06-03
  • 2019-09-15
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2013-12-15
  • 2011-10-10
  • 1970-01-01
相关资源
最近更新 更多