Last active
August 29, 2015 14:04
-
-
Save muhittin/a36b4d8c536d64e16f12 to your computer and use it in GitHub Desktop.
Regex check for e-mail address, url and phone number in given string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static function findEmail($string) | |
{ | |
$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i'; | |
preg_match_all($pattern, $string, $matches); | |
return (count($matches[0]) > 0); | |
} | |
public static function findUrl($string) | |
{ | |
$pattern = '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@'; | |
preg_match_all($pattern, $string, $matches); | |
return (count($matches[0]) > 0); | |
} | |
public static function findPhone($string) | |
{ | |
$pattern = '/[0-9\(\) ]{7,18}/i'; | |
preg_match_all($pattern, $string, $matches); | |
return (count($matches[0]) > 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment