Skip to content

Instantly share code, notes, and snippets.

@muhittin
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhittin/a36b4d8c536d64e16f12 to your computer and use it in GitHub Desktop.
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.
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