Validating many things in PHP is often done using regular expressions, but since those might be complicated to understand, versions of PHP later than 5.20 have new validating mechanism built in. It’s done using filter_var function. Here are some basic examples of both old (regular expression) and new (filter_var) validation functions:
Validating URL using regular expressions:
function isValidURL($url) { return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url); }
Validating URL using filter_var:
function isValidURL($url) { if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) return true; else return false; }
You will notice FILTER_VALIDATE_URL and FILTER_FLAG_HOST_REQUIRED flags in filter_var function. There are many more and here are some more real world examples
var_dump((bool) filter_var('http://www.website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)); var_dump((bool) filter_var('http://website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)); var_dump((bool) filter_var('www.website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)); var_dump((bool) filter_var('website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));
Output:
bool(true) bool(true) bool(true) bool(false)
Here are some most common URL validation flags explanation
Validating Email using regular expressions:
function isValidEmail($email) { return preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $email); }
Validating Email using filter_var:
function isValidEmail($email) { if (filter_var($email, FILTER_VALIDATE_EMAIL)) return true; else return false; }
Conclusion
Validating URLs, Emails, IPs and many other things using regular expressions is history now. Code for validations using filter_var is easier to understand easier to write and looks more php geeky. You don’t have to search for “working” regular expressions for your validations… It’s all there and it’s well documented and all you need is just need to use it.
More details about filter_var and it’s filters and flags can be found in here:
Can't download new apps to your phone because there isn't enough storage space? Lack of…
This issue started to appear on macOS 13 Ventura after recent Dropbox update. I would…
Since this morning (Feb 5th 2020) search just stopped working on my Windows 10 (version…
Many of you have run into a problem when you were working on some old…
After 10 years of running my main desktop computer on Windows 7 - time has…
I installed VestaCP for a clien on a new dedicated server running CentOS 7. Once…
View Comments
I think It is wrong!. var_dump((bool) filter_var('http://website', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)); will return true. It is wrong?.
You are right! I have fixed it the article above...
The regular expression will not work, if there is language-specifik letters, like ÆØÅ (Norwgeian) etc.
I strongly agree below post :
I think It is wrong!. var_dump((bool) filter_var(‘http://website’, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)); will return true. It is wrong?.
The regexp is not fixed. OMFG, lost 10 minutes on this pointless code.