How to remove HTML tags in PHP?

Guanhui
Release: 2023-03-01 15:18:02
Original
3653 people have browsed it

How to remove HTML tags in PHP?

PHP如何去掉HTML标签?

在PHP中可以使用“strip_tags()”函数去掉HTML标签,该函数作用是从字符串中去除HTML和PHP标记,其语法是“strip_tags(str) ”,其参数str代表的是要去除标记的字符串,返回值为处理后的字符串。

演示示例

Test paragraph.

Other text'; echo strip_tags($text); echo "\n"; // 允许

echo strip_tags($text, '

'); ?>

以上例程会输出:

使用示例

/si', trim($tags), $tags);
  $tags = array_unique($tags[1]);
   
  if(is_array($tags) AND count($tags) > 0) {
    if($invert == FALSE) {
      return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?@si', '', $text);
    }
    else {
      return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?@si', '', $text);
    }
  }
  elseif($invert == FALSE) {
    return preg_replace('@<(\w+)\b.*?>.*?@si', '', $text);
  }
  return $text;
}
?>
Copy after login
loadHTML($html_str, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD)){
    foreach ($xml->getElementsByTagName("*") as $tag){
      if (!in_array($tag->tagName, $allowed_tags)){
        $tag->parentNode->removeChild($tag);
      }else{
        foreach ($tag->attributes as $attr){
          if (!in_array($attr->nodeName, $allowed_attrs)){
            $tag->removeAttribute($attr->nodeName);
          }
        }
      }
    }
  }
  return $xml->saveHTML();
}
Copy after login

推荐教程:《PHP教程

The above is the detailed content of How to remove HTML tags in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!