Summarize how PHP deletes content inside HTML tags and tags

Source: Internet
Author: User

Often grilled others site article pit people; I mean the kind of bulk collection of the not to see the content, but will be used to delete the HTML tag function; Here are 3 different ways to use it;

$str = ' <div><p> here is P tag </p><a href= "" > here is a tag </a>< Br></div> ';

1: Delete all or keep the specified HTML tag

PHP comes with a function strip_tags to meet the requirements,

Method of Use: Strip_tags (String,allow);

String: Strings that need to be processed;

Allow: You need to keep the specified label, you can write more than one;

<?php Echo strip_tags ($str, ' <p><a> '); ?>//Output:<p> here is P tag </p><a href= "" > here is a tag </a>

The advantage of the secondary function is that it is simple and rough, but the disadvantage is obvious, if there is a lot of labels, and I just want to delete one of the specified one, and write a lot of labels that need to be kept;

2: Delete the specified HTML tag

Method of Use: Strip_html_tags ($tags, $STR);

$tags: Label to delete (array format)

$STR: The string to be processed;

<?php function Strip_html_tags ($tags, $str) {$html =array (); foreach ($tags as $tag) {$html []=]/(< (?: \ /". $tag." | ". $tag. ")        [^>]*>)/I ";     } $data =preg_replace ($html, ", $STR);       } Echo strip_html_tags (Array (' P ', ' img '), $STR); ?>//Output <div> here is P label <a href= "" > here is a tag </a><br></div>;

3: Delete the contents of tags and labels

Method of Use: Strip_html_tags ($tags, $STR);

$tags: Label to delete (array format)

$STR: The string to be processed;

<?php function Strip_html_tags ($tags, $str) {$html =array (); foreach ($tags as $tag) {$html []= '/(< $tag. *?>[\s|\s]*?<\/'. $tag. '        >)/';    } $data =preg_replace ($html, ", $STR);       } Echo strip_html_tags (Array (' A ', ' img '), $STR); ?>//Output <div><p> here is the P tag </p><br></div>;

Many web site articles will bring the site name and links, such as <a href= "http://www.baijunyao.com" > Bai Jun Remote Blog </a>; This function is the tyranny of this; Do not take this function to collect this station AH, or promise not to kill you;

4: The ultimate function, delete the specified label, delete or retain the contents of the tag;

Method of Use: Strip_html_tags ($tags, $str, $content);

$tags: Label to delete (array format)

$STR: The string to be processed;

$ontent: Whether to delete content within a label 0 preserve content 1 do not preserve content

  <?php    /**     *  delete a specified label and content       *  @param  array  $tags   Array of tags to delete      * @ param string  $str   Data sources      *  @param  string  $content   Whether to delete content within tags   default to 0 reserved content     1 do not keep content      *  @return   String     */    function strip_html_tags ($tags, $STR, $ content=0) {        if ($content) {              $html =array ();             foreach  ($tags  as  $tag)  {                  $html []= '/(< '. $tag. ') *?>[\s|\s]*?<\/'. $tag. ' >)/';             }            $ Data=preg_replace ($html, ", $str);        }else{              $html =array ();             foreach  ($tags  as  $tag)  {                  $html []=]/(< (?: \ /". $tag." | ". $tag. ") [^>]*>)/I ";            }              $data =preg_replace ($html,  ",  $str);         }        return  $data;     }    echo strip_html_tags (Array (' a '), $STR, 1);    ?>   //Output <dIv><p> here is the P tag </p><br></div>; 

The front is so much, in fact, the last function is dry;

Do not look at the following this one, nothing but a good color, I mainly take the picture as the cover of the article;

This article for Bai Jun Remote original article, reprinted without contact with me, but please specify from Bai Jun remote blog baijunyao.com

Summarize how PHP deletes content inside HTML tags and tags

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.