removing characters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

removing characters

Post by iffo »

suppose I have something like the following.


$phone="(212)435_7869)"

I want to make sure it has only number nothing else. Is there easy way to do that?
I want it to be like
$phone= 2124357869
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Try:

Code: Select all

$phone = "(209)45345444";
$length = strlen($phone);
$new_phone = "";
for ($i=0;$i<$length;$i++) {
 if (ord($phone[$i]) >= ord('0') && ord($phone[$i]) <= ord('9'))
  $new_phone .= $phone[$i];
}
echo $new_phone;
Last edited by Cameri on Mon Oct 23, 2006 11:52 am, edited 1 time in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

//untested

Code: Select all

$num = preg_replace('#[^\d\.]#','',$num);
Edit:

I just tried this with a decimal and it returned the decimal. If you try my code be sure to test for the decimal.
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Yeah, your untested version works for me.

Edit: Why would a phone number contain a dot? A extension?
Last edited by Cameri on Mon Oct 23, 2006 11:55 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hawleyjr wrote://untested

Code: Select all

$num = preg_replace('#[^\d\.]#','',$num);
\D matches a "non-digit".
and why a dot?
iffo wrote:I want to make sure it has only number nothing else.

Code: Select all

$num = preg_replace('#\D+#','',$num);
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Cameri wrote:Yeah, your untested version works for me.

Edit: Why would a phone number contain a dot? A extension?
I type phone number with dots all the time: 555.555.5555
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post by iffo »

thanks a lot, loop works, preg match will not work, because it could be any character...
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

iffo wrote:thanks a lot, loop works, preg match will not work, because it could be any character...
Did you try volka's modification?
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

I'm pretty sure volka's is way faster.
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post by iffo »

thanks again. I am trying to understand this

$num = preg_replace('#\D+#','',$num);

you said D replace non-digit
what is # signs for and +
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Im not sure about the #'s, but + means one or more of the preceeding match, and since it's \D, it means one or more non-digits.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You need a delimiter for pcre, perl compatible regular expressions.
That's just a character marking the start and end of a pattern, like " or ' mark the start or end of a string.
Can be / like in perl script sbut can also be #, ! and many more. I usually choose !

Code: Select all

$pattern = '!\D+!';
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post by iffo »

$num = preg_replace('#[^\d\.]#','',$num);
means keep dot and digits and replace rest with ''.
Post Reply