Improved email and url sanitation 06-08-2013, 08:33 PM
#1
Many people just uses PHP’s native functionality blindly thinking that everything will work smoothly, but that isn’t the case. PHP provides the FILTER_SANITIZE_EMAIL and FILTER_SANITIZE_URL filters to use with the filter functions. The problem here is that they also strip unicode characters, meaning that if you’re sanitizing a unicode url you will destroy it. So to solve this we cannot use the native sanitation, but we need to use preg_replace.
Many people is against the use of regular expressions when it comes to working with emails and urls, but keep in mind, we’re not validating. We’re only stripping away illegal characters, so the pattern is not complex at all.
So how is this enabling unicode? Well, we use \pL which accepts letters from all languages.
Email sanitation:
URL sanitation:
The patterns has been tested using the urls found here http://idn.icann.org/#The_example.test_names
I've also made a code snippet for each
Email
Url
Original source: My blog
Many people is against the use of regular expressions when it comes to working with emails and urls, but keep in mind, we’re not validating. We’re only stripping away illegal characters, so the pattern is not complex at all.
So how is this enabling unicode? Well, we use \pL which accepts letters from all languages.
Email sanitation:
Code:
$pattern = '/[^\pL\pN!#$%&\'\*\+\-\/\=\?\^\_`\{\|\}\~\@\.\[\]]/u';
$email = preg_replace($pattern, '', 'some@email.com');URL sanitation:
Code:
$pattern = '/[^\pL\pN$-_.+!*\'\(\)\,\{\}\|\\\\\^\~\[\]`\<\>\#\%\"\;\/\?\:\@\&\=\.]/u';
$url = preg_replace($pattern, '', 'http://www.someurl.com');The patterns has been tested using the urls found here http://idn.icann.org/#The_example.test_names
I've also made a code snippet for each
Code:
function sanitizeEmail($email)
{
$pattern = '/[^\pL\pN!#$%&\'\*\+\-\/\=\?\^\_`\{\|\}\~\@\.\[\]]/u';
return preg_replace($pattern, '', $email);
}Url
Code:
function sanitizeUrl($url)
{
$pattern = '/[^\pL\pN$-_.+!*\'\(\)\,\{\}\|\\\\\^\~\[\]`\<\>\#\%\"\;\/\?\:\@\&\=\.]/u';
return preg_replace($pattern, '', $url);
}Original source: My blog




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
Glad you like my blog