PHP preg_replace() pattern, string sanitization.

Posted by Otar on Stack Overflow See other posts from Stack Overflow or by Otar
Published on 2011-02-06T15:04:30Z Indexed on 2011/02/06 15:25 UTC
Read the original article Hit count: 272

Filed under:
|
|

I have a regex email pattern and would like to strip all but pattern-matched characters from the string, in a short I want to sanitize string...

I'm not a regex guru, so what I'm missing in regex?

<?php

$pattern = "/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i";

$email = 'contact<>@domain.com'; // wrong email

$sanitized_email = preg_replace($pattern, NULL, $email);

echo $sanitized_email; // Should be [email protected]

?>

Pattern taken from: http://fightingforalostcause.net/misc/2006/compare-email-regex.php (the very first one...)

© Stack Overflow or respective owner

Related posts about php

Related posts about regex