remove leading 1 in 10 digit phone numbers

given many lines of phone numbers in this format
16466512383
18002752273
13104285590
4917623423981
525544843633

want
6466512383
8002752273
3104285590
4917623423981
525544843633

only take off the leading 1 in american phone numbers
or phone numbers that are 10 digits

if there is not a better way to remove leading 1 from american phone numbers only then:
make 10 digit phone numbers 9 digits by removing leading 1
LVL 1
rgb192Asked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
This is the current standard for sanitizing and normalizing US telephone numbers.  It's packaged as a function.  You may or may not want all of the components, but hopefully the comments will enable you to choose the parts you need.

These numbers are not US phone numbers, so the function will return FALSE: 4917623423981 and 525544843633

You would probably want to handle FALSE returns separately.

<?php // RAY_phone_numbers.php
error_reporting(E_ALL);

// A FUNCTION TO VALIDATE A USA PHONE NUMBER AND RETURN A NORMALIZED STRING
// MAN PAGE: http://discuss.fogcreek.com/joelonsoftware3/default.asp?cmd=show&ixPost=102667&ixReplies=15
// MAN PAGE: http://www.nanpa.com/number_resource_info/index.html
function strtophone($phone, $format=FALSE, $letters=FALSE, $dlm='-')
{
    if ($letters)
    {
        // TURN INPUT LIKE 1-800-BIG-DOGS
        // INTO INPUT LIKE 1-800-244-3647
        $phone = strtoupper($phone);
        if (preg_match('/[A-Z]/', $phone))
        {
            $phone = strtr
            ( $phone
            , 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            , '22233344455566677778889999'
            )
            ;
        }
    }

    // DISCARD NON-NUMERIC CHARACTERS
    $phone = preg_replace('/[^0-9]/', NULL, $phone);

    // DISCARD A LEADING '1' FROM NUMBERS ENTERED LIKE 1-800-555-1212
    if (substr($phone,0,1) == '1') $phone = substr($phone,1);

    // IF LESS THAN TEN DIGITS, IT IS INVALID
    if (strlen($phone) < 10) return FALSE;

    // IF IT STARTS WITH '0' OR '1' IT IS INVALID, SECOND DIGIT CANNOT BE '9' (YET)
    if (substr($phone,0,1) == '0') return FALSE;
    if (substr($phone,0,1) == '1') return FALSE;
    if (substr($phone,1,1) == '9') return FALSE;

    // ISOLATE THE COMPONENTS OF THE PHONE NUMBER
    $ac = substr($phone,0,3); // AREA
    $ex = substr($phone,3,3); // EXCHANGE
    $nm = substr($phone,6,4); // NUMBER
    $xt = substr($phone,10);  // EXTENSION

    // ADD OTHER TESTS HERE AS MAY BE NEEDED - THESE ARE FOR LOCAL APPS
    if ($ac == '900') return FALSE;
    if ($ac == '976') return FALSE;
    if ($ex == '555') return FALSE;

    // IF NOT FORMATTED
    if (!$format) return $phone;

    // STANDARDIZE THE PRINTABLE FORMAT OF THE PHONE NUMBER LIKE 212-555-1212-1234
    $formatted_phone = $ac . $dlm . $ex . $dlm . $nm;
    if ($xt != '') $formatted_phone .= $dlm . $xt;
    return $formatted_phone;
}



// DEMONSTRATION OF THE FUNCTION IN ACTION.
if (!empty($_GET["p"]))
{
    // VALIDATE PHONE USING FUNCTION ABOVE
    if (!$phone = strtophone($_GET["p"], TRUE))
    {
        // FUNCTION RETURNS FALSE IF PHONE NUMBER IS UNUSABLE
        echo "BOGUS: {$_GET["p"]} ";
    }
    else
    {
        // SHOW THE FORMATTED PHONE
        echo "VALID: {$_GET["p"]} == $phone";
    }
}


// PUT UP A FORM TO TEST PHONE NUMBERS
function ph($p)
{
    echo "<br/><a href=\"{$_SERVER['PHP_SELF']}?p=" . urlencode($p) . "\">$p</a>" . PHP_EOL;
}
$form = <<<EOD
<form>
ENTER A PHONE NUMBER:
<input name="p" /><br/>
<input type="submit" />
</form>
TRY SOME OF THESE (CLICK OR COPY AND PASTE):
EOD;
echo $form;
ph('1-800-5551212');
ph('202-537-7560');
ph('202 537 7560');
ph('1-202-537-7560');
ph('(202) 537-7560');
ph('1.202.537.7560');
ph('123456789');
ph('703-356-5300 x2048');
ph('(212) 555-1212');
ph('1 + (212) 555-1212');
ph('1 (292) 226-7000');

Open in new window

Best regards, ~Ray
0
 
Jagadishwor DulalConnect With a Mentor Braces MediaCommented:
Here is a example that suits for you but I don't know how did you generate the lines??

<?php
$string="16466512383";
$counter=strlen($string);
if($counter>10){
	$find = substr($string, 0, 1);
	if($find==1){
		echo $res=substr($string,1);
	}
}

Open in new window

0
 
Julian HansenConnect With a Mentor Commented:
<?php
$number = '1233445566';

if ($number[0] == 1) echo substr($number,1);
?>

Open in new window

0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
ramyajanarthananConnect With a Mentor Commented:
Hope this meets your requirement

<?php
$phoneno='12234567890';
$length = strlen((string) $phoneno);
$first=substr($phoneno,0,1);
if($length>10&&$first==1)
{
$number=substr($phoneno,1,10);
print_r($number);
}
else {
print_r('invalid');	
}
exit;
?>

Open in new window

0
 
Beverley PortlockConnect With a Mentor Commented:
An alternative is a regular expression like so

$newNumber = preg_replace( '#1(.*?)#', '$1', $aPhoneNumber );

http://www.php.net/preg_replace

Using your test data...

<?php

$data = array( "16466512383",
               "18002752273",
               "13104285590",
               "4917623423981",
               "525544843633"
             );



foreach( $data as $aPhoneNumber ) {

     $newNumber = preg_replace( '#1(.*?)#', '$1', $aPhoneNumber );

     echo "Old number $aPhoneNumber is now $newNumber<br/>";
}

Open in new window


which produces

Old number 16466512383 is now 646652383
Old number 18002752273 is now 8002752273
Old number 13104285590 is now 304285590
Old number 4917623423981 is now 49762342398
Old number 525544843633 is now 525544843633
0
 
rgb192Author Commented:
I like Ray's solution the best.
All did logic in the presence of a number 1
0
All Courses

From novice to tech pro — start learning today.