Solved

remove leading 1 in 10 digit phone numbers

Posted on 2013-06-26
6
639 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 57

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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

707 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