preg help

This is a discussion on preg help within the PHP Language forums, part of the PHP Programming Forums category; I am not well versed on regular expressions, but have fumbled through enough to come up with these three preg_replace() ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-23-2008
William Gill
 
Posts: n/a
Default preg help

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);
Reply With Quote
  #2 (permalink)  
Old 04-24-2008
Alexey Kulentsov
 
Posts: n/a
Default Re: preg help

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";

?>
Reply With Quote
  #3 (permalink)  
Old 04-24-2008
William Gill
 
Posts: n/a
Default Re: preg help

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";
>
> ?>

Reply With Quote
  #4 (permalink)  
Old 04-25-2008
Alexey Kulentsov
 
Posts: n/a
Default Re: preg help

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?
Reply With Quote
  #5 (permalink)  
Old 04-25-2008
William Gill
 
Posts: n/a
Default Re: preg help

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:48 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0