PHP Filters HTML tags in a variety of ways

Source: Internet
Author: User
Tags chr html tags ord

Method One
<?php

The code is as follows Copy Code
echo strip_tags ("Hello <b>world!</b>");


Strip_tags---To remove html and PHP tags from strings

Syntax: string strip_tags (String str [, String allowable_tags])

Description

This function tries to remove all HTML and PHP tags from the given string, and if it is an incomplete or false label, there is an error, and the FGETSS () uses the same method to remove the tag.

The code is as follows Copy Code
$reg = '/(</?p>|<brs*/?>) |<.+?>/i ';
Echo preg_replace ($reg, ' $ ', $str);
?>

Filter Method Two

The code is as follows Copy Code
function delhtml ($STR) {//Clear HTML tags
$st =-1; Begin
$et =-1; End
$stmp =array ();
$stmp []= "&nbsp;";
$len =strlen ($STR);
for ($i =0; $i < $len; $i + +) {
$ss =substr ($str, $i, 1);
if (Ord ($SS) ==60) {//ord ("<") ==60
$st = $i;
}
if (Ord ($SS) ==62) {//ord (">") ==62
$et = $i;
if ($st!=-1) {
$stmp []=substr ($str, $st, $et-$st + 1);
}
}
}
$str =str_replace ($stmp, "", $str);
return $str;
}

Filtering Method Three

  code is as follows copy code

function Clear_html_label ($html)
{
$search = array ("' <script[^>]*?>.*?</script> ' si", "' <[/!] *? [^<>]*?> ' Si "," ' ([RN]) [s]+ ' "," ' & (quot| #34); ' I "," ' & (amp| #38); ' I "," ' & (lt| #60); ' I "," ' & (gt| #62); ' I "," ' & (nbsp| #160); ' I "," ' & (iexcl| #161); ' I "," ' & (cent| #162); ' I "," ' & (pound| #163); ' I "," ' & (copy| #169); ' I "," ' &# (d+); ' E ");
$replace = Array ("", "", "1", "" "," & "," < "," > "," ", Chr (161), Chr (162), Chr (163), Chr (169)," Chr (1) "); /p>

Return preg_replace ($search, $replace, $html);
}

Instance Application

$string = ' aaa<br/> <SCRIPT>FDSAFSA ';
echo Clear_html_label ($string);//aaa FDSAFSA

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.