PHPstrip_tags to retain multiple HTML tags, strip_tags tag_php tutorial

Source: Internet
Author: User
PHPstrip_tags: the strip_tags label. PHPstrip_tags: how to retain multiple HTML tags. This document describes how to retain multiple HTML tags by using the PHPstrip_tags function, you can use the second parameter to set the method for retaining multiple HTML tags without deleting PHP strip_tags. the strip_tags tag

This article describes how to retain multiple HTML tags by using the PHP strip_tags function. you can use the second parameter to set tags that do not need to be deleted, mainly involving the second parameter of strip_tags.

Strip_tags function

Syntax
String strip_tags (string str [, string allowable_tags])
Returns a string that removes HTML tags. you can use the second parameter to set tags that do not need to be deleted.

Usage:

Premise: if there is such a string,

The code is as follows:
$ Str ="

I'm fromHelping customers

";

1. do not retain any HTML tags. the code will be like this:
The code is as follows:
Echo strip_tags ($ str );
// Output: I am from the customer center

2. if only one tag is retained, you only need to write the string to the second parameter of strip_tags:

The code is as follows:
Echo strip_tags ($ str ,"");
// Output: I am from the customer center

3. retain

And... Multiple tags, separated by spaces, must be written to the second parameter of strip_tags:

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

");
// Output:

I'm fromHelping customers


What if you want to use php to delete a specific tag in the html tag?

This requires code implementation, as shown below:

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.        '[^>]*>([^<]*)
   /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'));

Http://www.bkjia.com/PHPjc/1133109.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133109.htmlTechArticlePHP strip_tags to retain multiple HTML tags, strip_tags label this article introduces the PHP strip_tags function to retain multiple HTML tags, you can use the second parameter to set do not need to delete...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.