PHP strip_tags methods for keeping multiple HTML tags, strip_tags tags _php tutorials

Source: Internet
Author: User

PHP strip_tags methods for keeping multiple HTML tags, strip_tags tags


This article describes the PHP strip_tags function to preserve multiple HTML tags, you can use the second parameter to set a label that does not need to be deleted, mainly related to the second parameter of Strip_tags

Strip_tags function

Grammar
String Strip_tags (String str [, String allowable_tags])
Returns a string that removes the HTML tag; You can use the second parameter to set a label that does not need to be deleted.

How to use:

Premise: If there is such a string now,

Copy the Code code as follows:
$str = "

I'm from the Help house .

";

1, do not keep any HTML tags, the code will be like this:
Copy the Code code as follows:
echo Strip_tags ($STR);
Output: I'm from the Help house.

2, to keep only one label, just write the string to the second parameter of Strip_tags:

Copy the Code code as follows:
Echo strip_tags ($str, "");
Output: I'm from the Help house.

3, to keep

With the. Multiple labels, you only need to separate multiple labels with a space and write to the second parameter of Strip_tags:

Copy the Code code as follows:
Echo strip_tags ($str, "

");
Output:

I'm from the Help house .


What if you want to use PHP to remove specific tags from HTML tags?

This requires code to be implemented, 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.        ' [^>]*> ([^<]*)
 
     /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.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133109.html techarticle PHP Strip_tags The method of keeping multiple HTML tags, strip_tags tags This article describes the PHP strip_tags function to keep multiple HTML tags, you can use the second parameter to set no 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.