Home  >  Article  >  Backend Development  >  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
WBOYOriginal
2016-07-12 08:50:38888browse

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 = "e388a4556c0f65e1904146cc1a846beeI am froma4b561c25d9afb9ac8dc4d70affff419305d4f929f21f7e9ebcdea53ac7dafd2Bangke Home5db79b134e9f6b82c0b36e0489ee08ed0d36329ec37a2cc24d42c7229b69747a51b364ec7b505771ce85379ba043e0e6, you only need to write the 3499910bf9dac5ae3c52d5ede7383485 string into the second parameter of strip_tags:

Copy code The code is as follows:
echo strip_tags($str, "3499910bf9dac5ae3c52d5ede7383485");
// Output: I am from305d4f929f21f7e9ebcdea53ac7dafd2Bangke Home5db79b134e9f6b82c0b36e0489ee08ed

3. To retain multiple tags of e388a4556c0f65e1904146cc1a846bee and a4b561c25d9afb9ac8dc4d70affff419..., 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, "e388a4556c0f65e1904146cc1a846bee a4b561c25d9afb9ac8dc4d70affff419");
// Output: e388a4556c0f65e1904146cc1a846beeI am froma4b561c25d9afb9ac8dc4d70affff419Bang Ke Home0d36329ec37a2cc24d42c7229b69747a94b3e26ee717c64999d7867364b1b4a3

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'));

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...
Statement:
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