Example analysis of strip_tags() in php that only filters a certain tag in the string

黄舟
Release: 2023-03-17 18:32:02
Original
2559 people have browsed it

We all know that the php strip_tags() function is used to filter out the html, php, and xml tags in the string. This function can only retain the desired html tags, But the specified html tag cannot be filtered out, so how to filter out the specified html tag? Today we will take you to a detailed introduction to strip_tags() in PHP to only filter a certain tag in the string!

php removes the specified html tags in the string. We cannot use the strip_tags() function, because this function can only retain the desired html tags, such as:

strip_tags($string); //去掉$string字符串中所以的html标签.
strip_tags($string,&#39;<div><img><em>&#39;); //去掉除了<div><img><em>以外的所有标签,即保留字符串中的div、img、em标签。
Copy after login

To remove the specified For html tags, we can only write a function ourselves. The function is as follows:

function strip_only_tags($str, $tags, $stripContent = FALSE) { 
  $content = &#39;&#39;; 
 
  if (!is_array($tags)) { 
    $tags = (strpos($str, &#39;>&#39;) !== false ? explode(&#39;>&#39;, str_replace(&#39;<&#39;, &#39;&#39;, $tags)) : array($tags)); 
    if (end($tags) == &#39;&#39;) { 
      // http://www.manongjc.com/article/1213.html
      array_pop($tags); 
    } 
  } 
 
  foreach($tags as $tag) { 
    if ($stripContent) { 
      $content = &#39;(.+<!--&#39;.$tag.&#39;(-->|s[^>]*>)|)&#39;; 
    } 
 
    $str = preg_replace(&#39;#<!--?&#39;.$tag.&#39;(-->|s[^>]*>)&#39;.$content.&#39;#is&#39;, &#39;&#39;, $str); 
  } 
 
  return $str; 
}
Copy after login

Parameter introduction:

$str refers to the string that needs to be filtered.

$tags refers to the html tags to be removed.

$stripContent indicates whether to remove the content in the tag. The default is False, which means the content in the tag will not be deleted.

Usage examples:

<?php
$string=&#39;<div><a href="http://www.manongjc.com">码农教程<em>斜体</em></a><strong>加粗</strong></div>&#39;; 
$target = strip_only_tags($string, array(&#39;a&#39;,&#39;em&#39;));//移除$string字符串内的a、em、b标签。 
var_dump($target);
$target = strip_only_tags($string, array(&#39;em&#39;),true); //移除$string字符串内的a、em、b标签,并移除标签里面的内容
var_dump($target);
?>
Copy after login

Summary:

I believe that friends will learn from this article and be familiar with strip_tags( in php ) only filters a certain tag in the string. I have a certain understanding. I hope it will be helpful to your work!

Related recommendations;

Detailed explanation of the shortcomings of the PHP function strip_tags

Detailed introduction of PHP common function strip_tags

php string function strip_tags() usage summary

The above is the detailed content of Example analysis of strip_tags() in php that only filters a certain tag in the string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template