PHP Super filter function

Source: Internet
Author: User
PHP Super filter function PHP Super filter function
Do you always go through the previous filtering code every time you want to filter?
Have you searched for PHP solutions to prevent filtering and attack?
Do you fully follow the 'filter inputs and avoid output' standard in the Web field?
In fact, these problems require frequent filtering out of input data. Therefore, based on the experience of writing so many codes, it is more reliable to filter the whitelist!
Of course, if you have any better code or something, please leave a message to discuss it!


I found that I have been writing some filtering problems recently, and I have been writing PHP for a year or two. I found that the filtering problem has always been a big problem, the following shows a general whitelist-based filter function,

If you cannot use it, contact my qq852208555 for further discussion!

Implementation of GBK GB2312 encoding

   

Below is the UTF-8 pattern :( mainly Chinese character match different)

AlFilter ($ str = NULL, $ operate, $ ext = NULL) {if (! $ Str) return 0; // matching mode $ pattern $ Cp = "\ x {4e00}-\ x {9fff}"; $ Dp = '0-9 '; $ Wp = 'A-zA-Z'; $ Tp = '#% % ^ & * ()-+ ='; $ _ p = '_'; $ pattern = "/^ ["; $ OArr = str_split (strtolower ($ operate); // The split operator if (in_array ('C', $ OArr) $ pattern. = $ Cp; if (in_array ('D', $ OArr) $ pattern. = $ Dp; if (in_array ('W', $ OArr) $ pattern. = $ Wp; if (in_array ('t', $ OArr) $ pattern. = $ Tp; if (in_array ('_', $ OArr) $ pattern. = $ _ p; if ($ ext) $ pattern. = $ ext; $ pattern. = "] + $/U "; if (! Preg_match ($ pattern, $ str) return 0; else return $ str ;}


Of course, you can only use the UTF-8 GBK transcoding in the past to match... and vice versa, but I think the trouble of course there is a better way to improve, welcome to discuss, don't hesitate to enlighten me!



Reposted from happy love my blog address: http://blog.csdn.net/lovehappying this article address

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.