strip_tags function usage of PHP common functions

Source: Internet
Author: User

A, definitions and usage

The Strip_tags () function removes the HTML, XML, and PHP tags in the string.

Strip_tags (String,allow)

Parameter description

String required. Specify the string to check.

Allow optional. Specify the allowed label. These labels will not be deleted.

Note: This function always strips HTML comments. This cannot be changed by allow parameters.

B, example

Example 1

Echo strip_tags ("Hello <b>world!</b>");

Output:

Hello world!

Example 2

Echo strip_tags ("Hello <b><i>world!</i></b>", "<b>");

Output:

Hello world!


Example 3, strip_tags () example


<?php$text = ' <p>test paragraph.</p><!--Comment--> other text '; Echo strip_tags ($text); echo "\ n" ;//Allow <p>echo strip_tags ($text, ' <p> '); >

The example above will output:

Test paragraph. Other text
<p>test paragraph.</p> Other text


Example 4:

<?php
$str = "Hello <b><i>world</i></b>!";
echo Strip_tags ($STR);
echo ' <br/> ';
Echo strip_tags ($str, ' <b><i> ');
?>

Output results:

Hello world!
Hello world!

Example 5

What if you want to use PHP to remove a specific label from an HTML tag?

This will require 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 '));

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.