in Best Practices, PHP 5

FIEO: Filtering Input with PHP’s Filter Functions

Brand-new PHP developers have drilled into their heads the concept of Filter Input, Escape Output (FIEO). This concept essentially insists that all user-provided content be filtered or escaped, without exception. With the delivery of PHP 5.2.0, this got a lot easier, because PHP included, by default, the Filter library.

Before the Filter library, doing something such as validating an email address often required an ugly regular expression along the lines of this:

$email = '';
$regexp = "/^&#91;^0-9&#93;&#91;A-z0-9_&#93;+(&#91;.&#93;&#91;A-z0-9_&#93;+)*&#91;@&#93;&#91;A-z0-9_&#93;+(&#91;.&#93;&#91;A-z0-9_&#93;+)*&#91;.&#93;&#91;A-z&#93;{2,4}$/";

if (preg_match($regexp, $email)) {
    echo "Email address is valid.";
} else {
    echo "Email address is invalid";

The filtering protocol makes this easy for us by providing a built-in filter that we can use to validate an email address:

Frustrated with your company’s development practices?

You don't have to be!

No matter what the issues are, they can be fixed. You can begin to shed light on these issues with my handy checklist.

Plus, I'll help you with strategies to approach the issues at the organization level and "punch above your weight."

Powered by ConvertKit


  1. Hi Brandon,

    It’s worth noting that filter_var returns the “filtered” variable or false. If you use it for validation and don’t check against === false then you might bump into the null/false/0 problem.

    Consider trying to validate the integer 0:-

    if(!filter_var(0, FILTER_VALIDATE_INT)){
    echo “Not a valid integer”; // huh, 0 is a valid int!

    It’s probably a good habit to get into use === check against false:-

    if(false === filter_var(0, FILTER_VALIDATE_INT)){
    echo “Not a valid integer”;

    I got bitten by this recently :P

  2. Brandon,

    As we know strip_tags() doesn’t stop a lot of malicious code so I used HTML Purifier instead.

    How good is the PECL filter FILTER_SANITIZE_STRING at stripping out cross-site scripting and other nasty stuff? Could I safely use it instead of HTML Purifier?

  3. Andy, I think it’s perfectly fine for stripping out HTML, and probably faster than whatever you’re using, just by virtue of being compiled into PHP.

  4. Great post, Brandon. I love the idea of filters, not only because they simplify and standardize code, but also because if they are wrong, it WILL get corrected and the benefit will come forth to all of us.

    There are literally thousands of email validation REGEX strings and almost all of them are wrong in one way or another, probably including mine. In the example above, the TLD is limited to 4 characters, and this is technically incorrect since “.museum” is a valid TLD. Practically speaking, I know of no museum that is not already a “.org” ;-)

    I have been filtering email addresses with this, and now I can spare my eyes the strain of reading that REGEX:

    function check_valid_email($email)

    if (!preg_match(‘/^[A-Z0-9_-][A-Z0-9._-]*@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,6}$/i’, $email)) return FALSE;

    $emaila = explode(‘@’, $email);
    if ( checkdnsrr($emaila[1],”MX”) || checkdnsrr($emaila[1],”A”) ) return TRUE;

    return FALSE;

    Does FILTER_VALIDATE_EMAIL give any insight into the routability of the domain?

  5. Thanks but I’ll stick to using my own classes simply because the work is already done; I certainly ain’t gonna gut out my own script just to shave a few millionths of a second off what is already optimised script.

    Besides… I firmly believe these filters arrived way too late; they should have been with PHP since version 4 but…

  6. Herman, I’m not sure I buy the people at HTML Purifier as unbiased sources of information. I’d personally be wary about including 842 items in my application. HTML Purifier is almost 4.5 MB alone.

    Les, no need to gut your existing script. This is just one option out of many for securing your application. I agree, the filters should have been included sooner.

  7. @Les

    Agreed that they should have been added sooner, but personally, I look forward to stripping out some of my (potentially incorrect) code in favor of this. The speed is there but then it’s one more thing that I don’t have to worry about and I can spend my time on other things.

  8. Hi Brandon,
    Thanks, I just implemented a function parsing mailaddresses using ‘your’ filtering technique. Real nice :)

  9. > HTML Purifier is almost 4.5 MB alone

    I would stay away from HTML Purifier; not because it’s badly developed but as stated, it’s not exactly compact and from my own experience there are perforamce concerns.

    I did suggest to the developers that they break it down into more modular, manageable components a while back, giving the end user greater control and flexibility over the level of purification they do.

    Also, it’s way over the top for the most applications; I beg to differ that there are [now] better options available.

  10. 1st of all good point Adrian …. title of this post should say ‘validating email with PHPs filter function’ ;-)

    but jokes aside, the only thing that worries me about such innovations (they should be in php for ages) is that it always makes me wonder. How actually good are these functions?

    I love to use open source as i save time but sometimes i just dont know what is in there.

    With email there are all those different encodings of domain names and special chars in non-english languages. Its not that simple any more. Will they be accepted or not? How well unit tested is it? (and i dont mean coverage which does not mean anything in this case!). Why did they not add any information (doc says: ‘Validates value as e-mail.’)

    Finally, what if im not happy with it any more? What if there is bug or just filter is crap? Your approach does not isolate your code. So what i would do is add my own wrapper. If i change my mind i will just replace implementation not ripping entire site again.

    In addition i would add a few simple unit tests to make sure i know what to expect and i can easily replace implementation without major risk or waste of time.

    With PHP libs its a bit different story. I can easily lookup whats under the hood thats why i like zend framework so much :-) but reading PHP extensions code is usually much more frustrating. But i sill would wrap whenever possible.

    To summarize …. yes its super cool they added it and that people can use it but its totally not cool you dont know what you get ;-)


  11. I have started using HTMLPurifier on a project I’m working on and yes it’s heavy. I don’t plan on people POSTing stuff too much, so I’m not too worried about overhead.

    However, I would like to hear suggestions of other libraries that I could use, should I find out HTMLPurifier takes up too many resources.



  12. Even including the HTML Purifier code through the require() or include() statement has a performance hit, because the compiler must still compile it. So its heft might still present a problem, even if your users don’t actually make use of the code. Something to keep in mind.

  13. In that case, you just slap APC on the server, don’t you ?

  14. You’ll still face some issues with performance, though they won’t be as bad.

    Every time you invoke the PHP parser, APC will stat() the files included (and the files included by that script and so on) to see if they’ve changed. Chances are good that they haven’t, but APC still has to check (unless you disable that functionality as I’ve described here:

    Additionally, as APC fills up, it will dump files off the memory to ensure it doesn’t overrun its limit. This might mean that some files get cached, then dumped, then cached again, etc.

    You might be able to solve this by writing an autoload function (see and but as soon as you hit the HTML Purifier script it will include what it’s been written to include unless you strip out the includes, which would make upgrading difficult.

    I think a lot of the time you don’t need the functionality of something as robust as HTML Purifier, and leaving it out is better than including it and then trying to manage the performance and/or access questions that arise. You only need it in select circumstances (namely, when saving input to the database or getting it from the database) so you might be better off using the Lite version, rolling your own, using the filtering functions, or including it ONLY when ABSOLUTELY necessary.

  15. The biggest attraction for HTML Purifier is that it works at one of the most critical jobs in an application, namely it stops malicious code –

    I don’t have any confidence in the PHP filters for this job and I’m not really sure I want to start experimenting with my own filter for such a crucial job when a tried and tested script is already out there.

  16. I agree HTML Purifier does an excellent job but in my opinion not at the cost of performance.

    To get around any security issues what I do is to encode the POSTed data (coming from TEXTAREAs) with base64 and leave it to a human to clean up any mess etc.

    Not a perfect solution (you can’t sort or search data) by any means but it ain’t going to 1) break your box and 2) piss off your host.

  17. I find the syntax of the filter ext to be a little… well ugly. Who wants to remember all those constant names? So in my case I wrote a wrapper class around the extension with method names like sanitizeString, validateEmail, etc… which is easier for me to remember.

    I also find HTML Purifier to be too big.

Comments are closed.