Solved

remove leading 1 in 10 digit phone numbers

Posted on 2013-06-26
6
538 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 51

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 108

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now