PHP如何去掉HTML标签?
在PHP中可以使用“strip_tags()”函数去掉HTML标签,该函数作用是从字符串中去除HTML和PHP标记,其语法是“strip_tags(str) ”,其参数str代表的是要去除标记的字符串,返回值为处理后的字符串。
演示示例
Test paragraph. Other text'; echo strip_tags($text); echo "\n"; // 允许
以上例程会输出:
Test paragraph. Other textTest paragraph.
Other 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.*?>.*?\1>@si', '', $text); } else { return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?\1>@si', '', $text); } } elseif($invert == FALSE) { return preg_replace('@<(\w+)\b.*?>.*?\1>@si', '', $text); } return $text; } ?>
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(); }
推荐教程:《PHP教程》
以上是PHP如何去掉HTML标签?的详细内容。更多信息请关注PHP中文网其他相关文章!