-2

I'm coding in php and I would be happy if someone could help me with a regex that would validate phone numbers in the format +233 245 245 245 or 0245 245 245.

I would also want the phone number to be validated whether or not the spaces are included. Thanks a lot in advance.

Osei-Bonsu Christian
  • 2,935
  • 1
  • 15
  • 8

1 Answers1

0

Something simple like this would work:

$string = "(232) 555-5555";
$expr = '/^\+?(\(?[0-9]{3}\)?|[0-9]{3})[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{4}$/';
if (preg_match($expr, $string) == 1)
{
echo "number validated successfully.";
}
Chris
  • 805
  • 6
  • 19