removing characters
Moderator: General Moderators
removing characters
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
$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
- Cameri
- Forum Commoner
- Posts: 87
- Joined: Tue Apr 12, 2005 4:12 pm
- Location: Santo Domingo, Dominican Republic
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.
//untested
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.
Code: Select all
$num = preg_replace('#[^\d\.]#','',$num);I just tried this with a decimal and it returned the decimal. If you try my code be sure to test for the decimal.
\D matches a "non-digit".hawleyjr wrote://untestedCode: Select all
$num = preg_replace('#[^\d\.]#','',$num);
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);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 !
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+!';