|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I am not well versed on regular expressions, but have fumbled through
enough to come up with these three preg_replace() statements (I'm not even sure how the third one works, but it does)to reduce a form field value to the '(###) ###-####' U.S. phone format. My question, can I combine them into one? $phone=preg_replace('/\D/','',$text); // strip all non-digits $phone=preg_replace('/^[0-1]/','',$text); // strip leading 0 or 1 $phone=preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/', '(\1) \2-\3', $text); |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
William Gill wrote:
> I am not well versed on regular expressions, but have fumbled through > enough to come up with these three preg_replace() statements (I'm not > even sure how the third one works, but it does)to reduce a form field > value to the '(###) ###-####' U.S. phone format. My question, can I > combine them into one? > > $phone=preg_replace('/\D/','',$text); // strip all non-digits > $phone=preg_replace('/^[0-1]/','',$text); // strip leading 0 or 1 > $phone=preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. > ]?([0-9]{4})/', '(\1) \2-\3', $text); no problem: <?php // only 10 digits plus 0 or 1 at begin $phone1="sf fd f0 (1-2-3) - 45 6 - 7 89-0 ff";$phone2=$phone1; // two expression example $phone2=preg_replace('/^(\\d{3})(\\d{3})(\\d{4})$/','(\\1) \\2-\\3', preg_replace('/(?:^\\D*[01]|\\D)/','',$phone2)); echo $phone2."\n"; // one expression example $phone1=preg_replace('/^\\D*[01]?'.str_repeat('\\D*(\\d)',10).'\\D*$/','(\\1\\2\\3) \\4\\5\\6-\\7\\8\\9\\10',$phone1); echo $phone1."\n"; ?> |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Alexey Kulentsov wrote:
> no problem: > > <?php > > // only 10 digits plus 0 or 1 at begin I am removing 0 and 1 from the beginning because 1) I don't need to store 1 + phonenumber, just phonenumber, and 2) no valid area code begins with 0 or 1. > $phone1="sf fd f0 (1-2-3) - 45 6 - 7 89-0 ff";What is this? and where do you use it? It just produces the string "sf fd f0 (1-2-3) - 45 6 - 7 89-0 ff" which I don't see you use anywhere.> $phone2=$phone1; > > // two expression example > $phone2=preg_replace('/^(\\d{3})(\\d{3})(\\d{4})$/','(\\1) \\2-\\3', > preg_replace('/(?:^\\D*[01]|\\D)/','',$phone2)); > > echo $phone2."\n"; > > // one expression example > $phone1=preg_replace('/^\\D*[01]?'.str_repeat('\\D*(\\d)',10).'\\D*$/','(\\1\\2\\3) > \\4\\5\\6-\\7\\8\\9\\10',$phone1); > doesn't seem to work for me. I realized my example was wrong: $phone=preg_replace('/\D/','',$text); // strip all non-digits $phone=preg_replace('/^[0-1]/','',$text); // strip leading 0 or 1 $phone=preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/', '(\1) \2-\3', $text); should have been: $phone=preg_replace('/\D/','',$text); // strip all non-digits $phone=preg_replace('/^[0-1]/','',$phone); // strip leading 0 or 1 $phone=preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/', '(\1) \2-\3', $phone); > echo $phone1."\n"; > > ?> |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
William Gill wrote:
>> $phone1="sf fd f0 (1-2-3) - 45 6 - 7 89-0 ff";> > What is this? and where do you use it? It just produces the string "sf > fd f0 (1-2-3) - 45 6 - 7 89-0 ff" which I don't see you use anywhere.This it phone number with some junk inside to test example. Output is (123) 456-7890 (123) 456-7890 > doesn't seem to work for me. I realized my example was wrong: I always test my examples before posting. Did you try just copy example from post and run it? |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Alexey Kulentsov wrote:
> William Gill wrote: >>> $phone1="sf fd f0 (1-2-3) - 45 6 - 7 89-0 ff";>> >> What is this? and where do you use it? It just produces the string "sf >> fd f0 (1-2-3) - 45 6 - 7 89-0 ff" which I don't see you use anywhere.> > This it phone number with some junk inside to test example. Output is Somehow I missed that. >> doesn't seem to work for me. I realized my example was wrong: > I always test my examples before posting. Did you try just copy example > from post and run it? I must have cut and pasted wrong, because now when I c&p into a fresh file it works, thanks. |
|
![]() |
| Outils de la discussion | |
|
|