PHP HTML string filter function _php Tutorial

Source: Internet
Author: User
A custom function

The code is as follows

function striphtml ($string) {
$pattern =array (" ' Si "," ' ]*?>.*? ') 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)");
Return Preg_replace ($pattern, $replace, $STR);
}

In addition to the functions defined above, there is a PHP-capable function for filtering HTML: Strip_tags (String) so that you can filter out all the HTML tags.

If you want to filter out all the HTML tags except, you can write this:

The code is as follows

Strip_tags (String, "");

Filter except

Xxx

All HTML tags, you can write:

The code is as follows

Strip_tags (String, "

");

Find an anti-SQL injection function on the web

The code is as follows

PHP Batch filter Post,get sensitive data
if (GET_MAGIC_QUOTES_GPC ()) {
$_get = Stripslashes_array ($_get);
$_post = Stripslashes_array ($_post);
}

Function Stripslashes_array (& $array) {
while (list ($key, $var) = each ($array)) {
if ($key! = ' argc ' && $key! = ' argv ' && (Strtoupper ($key)! = $key | | ". Intval ($key) = =" $key ")) {
if (is_string ($var)) {
$array [$key] = stripslashes ($var);
}
if (Is_array ($var)) {
$array [$key] = Stripslashes_array ($var);
}
}
}
return $array;
}
//--------------------------

Replace the HTML footer tag for the filtering service Www.111cn.net
//--------------------------
function Lib_replace_end_tag ($STR)
{
if (empty ($STR)) return false;
$str = Htmlspecialchars ($STR);
$str = Str_replace ('/', "", $str);
$str = Str_replace ("\", "", $str);
$str = Str_replace (">", "", $str);
$str = Str_replace ("<", "", $str);
$str = Str_replace ("", "" ", $str);
$str = Str_replace ("", "" ", $str);
$str =str_replace ("Select", "select", $str);
$str =str_replace ("Join", "join", $STR);
$str =str_replace ("union", "union", $STR);
$str =str_replace ("where", "where", $str);
$str =str_replace ("Insert", "Insert", $STR);
$str =str_replace ("delete", "delete", $str);
$str =str_replace ("Update", "Update", $STR);
$str =str_replace ("like", "like", $STR);
$str =str_replace ("Drop", "drop", $str);
$str =str_replace ("Create", "create", $STR);
$str =str_replace ("Modify", "Modify", $str);
$str =str_replace ("rename", "Rename", $str);
$STR =str_replace ("Alter", "Alter", $STR);
$str =str_replace ("cas", "cast", $STR);
$str =str_replace ("&", "&", $STR);
$str =str_replace (">", ">", $str);
$str =str_replace ("<", "<", $str);
$str =str_replace ("", Chr (+), $str);
$str =str_replace ("", Chr (9), $STR);
$str =str_replace ("", Chr (9), $STR);
$str =str_replace ("&", CHR, $STR);
$str =str_replace ("'", Chr (), $STR);
$str =str_replace ("
, Chr (+), $str);
$str =str_replace ("'" "," ' ", $str);
$str =str_replace ("CSS", "'", $str);
$str =str_replace ("CSS", "'", $str);

return $str;

}

How to use

The code is as follows

The references are directly like this:
$xxx = htmlspecialchars ($_post[' xxx ');
Or
$xxx = htmlspecialchars ($_get[' xxx ');

http://www.bkjia.com/PHPjc/727582.html www.bkjia.com true http://www.bkjia.com/PHPjc/727582.html techarticle A custom function code is as follows functions striphtml ($string) {$pattern =array (' script[^]*?. *?/script ' Si, ' style[^]*? *?/style ' Si, ' [/!] *? [^]*?' Si, ' ([RN]) [s]+ ', ' (quot| #34); ' I ...

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