我看到了很多用于删除特定标签(或许多指定标签)的表达式,以及一种只保留一个特定标签的表达式,但我还没有找到一种在PHP中排除多个标签后删除其他所有标签的方法(即删除除了p、b、i、u、a、ul、ol、li
之外的所有标签)。我对正则表达式不是很熟悉,所以我需要帮手。 :) 谢谢!
我看到了很多用于删除特定标签(或许多指定标签)的表达式,以及一种只保留一个特定标签的表达式,但我还没有找到一种在PHP中排除多个标签后删除其他所有标签的方法(即删除除了p、b、i、u、a、ul、ol、li
之外的所有标签)。我对正则表达式不是很熟悉,所以我需要帮手。 :) 谢谢!
你可以使用strip_tags
函数来完成这个操作。
¶ strip_tags — 从字符串中去除 HTML 和 PHP 标记
strip_tags($contant,'tag you want to allow');
喜欢
strip_tags($contant,'<code><p>');
<link rel="canonical" href="http://www.ann24h.com/2017/10/blog-post_89.html">
- Msy Marina$html_value = strip_tags($contente, ['code', 'p']);
- Mohamad Hamoudaystrip_tags()
的作用就是去除 HTML 标签。
strip_tags
函数没有选项可以去除非允许标签内的内容。如果有这个选项,该函数会更加通用。 - coder.in.me您可以使用可选的第二个参数来指定不应被剥离的标记。这些可以作为字符串给出,或者从PHP 7.4.0开始,可以作为数组。
- Rufinusstrip_tags
仍然应该是首选方法。// tags separated by vertical bar
$strip_tags = "a|strong|em";
// target html
$html = '<em><b>ha<a href="" title="">d</a>f</em></b>';
// Regex is loose and works for closing/opening tags across multiple lines and
// is case-insensitive
$clean_html = preg_replace("#<\s*\/?(".$strip_tags.")\s*[^>]*?>#im", '', $html);
// prints "<b>hadf</b>";
echo $clean_html;