Solved

# remove leading 1 in 10 digit phone numbers

Posted on 2013-06-26
Medium Priority
755 Views
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
Question by:rgb192

LVL 15

Assisted Solution

Jagadishwor Dulal earned 400 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);
}
}
``````
0

LVL 62

Assisted Solution

Julian Hansen earned 400 total points
ID: 39277242
``````<?php
\$number = '1233445566';

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

LVL 4

Assisted Solution

ramyajanarthanan earned 400 total points
ID: 39277440

``````<?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;
?>
``````
0

LVL 34

Assisted Solution

Beverley Portlock earned 400 total points
ID: 39277561
An alternative is a regular expression like so

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

http://www.php.net/preg_replace

``````<?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/>";
}
``````

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 111

Accepted Solution

Ray Paseur earned 400 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'
)
;
}
}

\$phone = preg_replace('/[^0-9]/', NULL, \$phone);

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');
``````
Best regards, ~Ray
0

LVL 1

Author Closing Comment

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

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.