Home > Backend Development > PHP Tutorial > PHP strip_tags method to retain multiple HTML tags, strip_tags tag_PHP tutorial

PHP strip_tags method to retain multiple HTML tags, strip_tags tag_PHP tutorial

WBOY
Release: 2016-07-12 08:50:38
Original
893 people have browsed it

PHP strip_tags method to retain multiple HTML tags, strip_tags tags

This article introduces the method of PHP strip_tags function to retain multiple HTML tags. You can use the second parameter to set The tags that need to be deleted mainly involve the second parameter of strip_tags

strip_tags function

Grammar
string strip_tags ( string str [, string allowable_tags] )
Returns a string with HTML tags removed; you can use the second parameter to set tags that do not need to be removed.

How to use:

Premise: If there is such a string now,

Copy code The code is as follows:
$str = "

I am fromBangke Home

1, do not retain any HTML tags, the code will be like this:
Copy code The code is as follows:
echo strip_tags($str);
// Output: I am from Bangke Home

2. If you only retain one tag , you only need to write the string into the second parameter of strip_tags:

Copy code The code is as follows:
echo strip_tags($str, "
");
// Output: I am from
Bangke Home

3. To retain multiple tags of

and ..., you only need to separate multiple tags with spaces and write them to the second parameter of strip_tags:

Copy code The code is as follows:
echo strip_tags($str, "

");
// Output:

I am fromBang Ke Home


What if you want to remove specific tags in html tags using php?

This requires code to implement, as follows:

function strip_selected_tags($text, $tags = array()) {
  $args = func_get_args();
  $text = array_shift($args);
  $tags = func_num_args() > 2 ? array_diff($args, array($text)) : (array) $tags;
  foreach($tags as $tag) {
    if (preg_match_all('/<'.$tag.
        '[^>]*>([^<]*)</'.$tag.
        '>/iu', $text, $found)) {
      $text = str_replace($found[0], $found[1], $text);
    }
  }

  return preg_replace('/(<('.join('|', $tags).
    ')( | |.)*/>)/iu', '', $text);
}

$str = "[url="] 123[/url]";
    echo strip_selected_tags($str, array('b'));
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133109.htmlTechArticlePHP strip_tags method of retaining multiple HTML tags, strip_tags tag This article introduces the PHP strip_tags function to retain multiple HTML tags Method, you can use the second parameter to set the setting without deletion...
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