PHP removes the specified HTML tag method summary _php Tutorial

Source: Internet
Author: User
In PHP, we use the most commonly used to specify HTML tags can be used directly with the Strip_tags function to replace, use it to filter all the HTML tags oh, let me introduce you in addition to this function other methods.

Sometimes we need to save the HTML tab to the database, but some occasions need to take no HTML tag of the pure data, this time will be the HTML tag data processing, the HTML tags are removed. Usually use Htmlspecialchars () to filter the HTML, but the HTML characters escaped, the final display is the HTML source code, the use of strip_tags () can be used to remove HTML tags.


PHP default functions have removed the specified HTML tag, named Strip_tags, which is useful in some situations.

Strip_tags


Strip_tags-strip HTML and PHP tags from a string


String Strip_tags (String str [, String allowable_tags])

Disadvantages:


This function only retains the desired HTML tag, which is the argument string allowable_tags.

The parameters of this function allowable_tags other usages.

The code is as follows Copy Code


Strip_tags ($source, "); Remove the HTML tag so.
Strip_tags ($source, ") preserves the div, img, em tags in the string.

If you want to remove the specified label for the HTML. Then this function will not meet the requirements.


So I used this function.

The code is as follows Copy Code

function Strip_only_tags ($str, $tags, $stripContent = FALSE) {
$content = ";

if (!is_array ($tags)) {
$tags = (Strpos ($str, ' > ')!== false? Explode (' > ', Str_replace (' < ', ', $tags)): Array ($tags));
if (end ($tags) = = ") {
Array_pop ($tags);
}
}

foreach ($tags as $tag) {
if ($stripContent) {
$content = ' (. + |s[^>]*>) |) ';
}

$str = Preg_replace (' # |s[^>]*>) '. $content. ' #is ', ', $str);
}

return $str;
}


Parameter description


$str-refers to a string that needs to be filtered, such as HTML tags such as div, p, em, IMG, and so on.
$tags-refers to the need to remove the specified HTML tags, such as A, IMG, p, and so on.
$stripContent = false-Removes the contents of the tag, such as deleting the entire link, and defaults to FALSE, which means that the contents of the tag are not deleted.

Instructions for use

The code is as follows Copy Code


$target = Strip_only_tags ($source, Array (' A ', ' em ', ' B ')), remove the A, EM, and B tags within the $source string.

$source = ' This a example fromlixiphp!
';

$target = Strip_only_tags ($source, Array (' A ', ' em '));

Target results
This a example from!

Other options

The code is as follows Copy Code


Remove BR Mark
function strip ($STR)
{
$str =str_replace ("
"," ", $str);
$str =htmlspecialchars ($STR);
Return Strip_tags ($STR);
}
?>


A custom function

/

The code is as follows Copy Code
**
* Remove HTML tags
*
* @access Public
* @param string str
* @return String
*
*/
function deletehtml ($STR) {
$str = Trim ($STR); Clear spaces on both sides of a string
$str = Strip_tags ($str, "

"); Use PHP's own functions to clear the HTML format. Keep P Tags
$str = Preg_replace ("/t/", "", $str); Use regular expressions to match what needs to be replaced, such as: spaces, wrap, and replace with empty.
$str = Preg_replace ("/rn/", "", $str);
$str = Preg_replace ("/r/", "", $str);
$str = Preg_replace ("/n/", "", $str);
$str = Preg_replace ("//", "", $str);
$str = Preg_replace ("//", "", $str); Match spaces in HTML
Return trim ($STR); return string
}

http://www.bkjia.com/PHPjc/631529.html www.bkjia.com true http://www.bkjia.com/PHPjc/631529.html techarticle in PHP, we use the most commonly used to specify the HTML tag can be used directly with the Strip_tags function to replace, use it to filter all the HTML tags oh, let me introduce you in addition to this function ...

  • 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.