PHP Script to validate a phone number

This is a fast and simple script to validate a phone number.

The purpose of this script is to keep things simple.  The scripts cleans input whether it is passed as a numeric value or as a string.  It then validates it’s count of numbers found present in the value, then returns only the numbers or a false if it is not a proper phone number.

When you are validating a phone number, there is the possibility of an extension being added.  Since the extension can be added using any number of characters, we have decided to leave the extension validation off for now.

 * Removes everything but numbers, then checks its size to ensure 10 or 7 numbers.
 *   NOTE: Does not support extensions
 * @param $phone mixed is the phone number but is treated as a string
 * @param $ext boolean if set to true return an array with extension separated
 * @return
 function isPhone($phone, $ext = false) {
    // Strips non numeric values out
    $numbers = preg_replace("%[^0-9]%", "", $phone );

    // Get the length of numbers supplied
    $length = strlen($numbers);

    // Validate size - must be 10 or 7
    if ( $length == 10 || $length == 7 ) {
        return $numbers;

    // Was not a good number
    return false;




Posted in , .