Solved

remove leading 1 in 10 digit phone numbers

Posted on 2013-06-26
6
587 Views
Last Modified: 2013-07-12
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
0
Comment
Question by:rgb192
6 Comments
 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 100 total points
ID: 39277234
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
 
LVL 54

Assisted Solution

by:Julian Hansen
Julian Hansen earned 100 total points
ID: 39277242
<?php
$number = '1233445566';

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

Open in new window

0
 
LVL 4

Assisted Solution

by:ramyajanarthanan
ramyajanarthanan earned 100 total points
ID: 39277440
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 100 total points
ID: 39277561
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 100 total points
ID: 39278276
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
 

Author Closing Comment

by:rgb192
ID: 39322758
I like Ray's solution the best.
All did logic in the presence of a number 1
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 62
php convert date format 3 24
AWS EC2 & RDS Instance 5 36
How has this flight information been added to this website? 7 25
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question