0

I have phone numbers that I want to format

And I have a pattern matcher that breaks down the numbers into a 10 digit format, and then applies dashes. It works most of the time. However Im having an issue with certain numbers.

$trimmed = trim(preg_replace('/\s+/', '', $v->cust_num));
$tendigit = str_replace(array( '(', ')','-',' ' ), '', $trimmed);
$num = substr($tendigit,0,3)."-".substr($tendigit,3,3)."-".substr($tendigit,6,4);

This will change (555)555 5555, or 555-555 5555 or 5555555555 or (555)-555-5555 or 555-555-5555 to my format of 555-555-5555

However, I came across a few entries in my database, that dont seem to want to change.

One of the bad entries is this one. It contains two white spaces infront of the 4.

   4-035-0100

When it runs through $trimmed, and I output $tendigit...it outputs

  40350100 

as expected. But then when I apply $num to it. It goes back to

 4-035-0100

I would at least expect it to be

 403-501-00

It seems there is some hidden whitespace in it, that my preg_replace, trim, and str_replace are not attacking.

Any ideas??

Thanks

pdoherty926
  • 9,895
  • 4
  • 37
  • 68
Kylie
  • 11,421
  • 11
  • 47
  • 78
  • 1
    Is it in UTF? It might have some unicode symbols in front. Look at http://stackoverflow.com/questions/4166896/trim-unicode-whitespace-in-php-5-2 Try to use this instead of `trim` - `$str = preg_replace('/^[\p{Z}\s]+|[\p{Z}\s]+$/u','',$str);` – Cheery Oct 31 '14 at 21:11
  • a better solution might be to check to see if there are 10 numbers, and if there's not, kick the form back for reentry – Jeff Hawthorne Oct 31 '14 at 21:18
  • What results from `strlen($tendigit)` on that value? – Michael Berkowski Oct 31 '14 at 21:19
  • Please run this code and post what it outputs: `foreach(str_split($v->cust_num) as $char){echo $char.'='.ord($char)."\n";}` – codeaken Oct 31 '14 at 21:28
  • I ran that. and the ones that dont work output �=160 �=194 in front of the numbers. The two 'whitespaces' I guess are these characters? – Kylie Oct 31 '14 at 21:39
  • That is correct, your code is not filtering out those values – codeaken Oct 31 '14 at 21:44
  • @Cheery your code worked. Post it as the asnwer :) – Kylie Oct 31 '14 at 21:44
  • @KyleK Thanx, but it is a duplicate of http://stackoverflow.com/questions/4166896/trim-unicode-whitespace-in-php-5-2 – Cheery Oct 31 '14 at 21:45

2 Answers2

0

You can condense your code a little:

$tendigit = preg_replace('/[^\d]/', '', $v->cust_num);
$num = substr($tendigit,0,3)."-".substr($tendigit,3,3)."-".substr($tendigit,6,4);

Though, you should add in some conditions to check that the phone number actually has 10 digits too:

$tendigit = preg_replace('/[^\d]/', '', $v->cust_num);
if(strlen($tendigit == 10)){
    $num = substr($tendigit,0,3)."-".substr($tendigit,3,3)."-".substr($tendigit,6,4);
} else {
    // catch your error here, eg 'please enter 10 digits'
}

The first line removes any 'non-digit' [^\d].

The conditional statement checks if the $tendigit variable has 10 digits in it.

If it does, then it uses your code to parse and format.

If it doesnt, then you can catch an error.

Jonathan
  • 1,542
  • 3
  • 16
  • 24
0

The code below works, I have tried it with the special characters we discovered in the comments. Basically, the regex removes everything that isnt a number (0-9) and then uses your original formatting.

$trimmed = preg_replace('/\D+/', '', $v->cust_num);
$num = substr($trimmed,0,3)."-".substr($trimmed,3,3)."-".substr($trimmed,6,4);
codeaken
  • 844
  • 1
  • 8
  • 20
  • Nice...reduced to two lines...I like – Kylie Oct 31 '14 at 22:06
  • what happens when someone enters a 6 digit number or a 5 digit number? – Jonathan Oct 31 '14 at 22:08
  • it just makes it 555-55 or 555-555. Which is fine, because any values not matching 555-555-5555 get flagged as INVALID, which after the bulk upload of leads, the user gets alerted to any numbers flagged as such. and they can either delete it, or change it – Kylie Oct 31 '14 at 22:12