Page 1 of 1

preg_replace all but digits and semicolon :

Posted: Mon Aug 24, 2009 4:09 am
by lovelf

Code: Select all

 <?php
$string='255:125 bear';
 
$nvar = preg_replace('([\D])', '', $string);
echo $nvar; //returns 255125
$nvar = preg_replace('[\w]', '', $string);
echo $nvar; // returns :
?>
How is it done so that 255:125 is returned?

Re: preg_replace all but digits and semicolon :

Posted: Mon Aug 24, 2009 4:16 am
by jackpf

Code: Select all

preg_replace('/[^0-9\:]/', null, $str);
Should do it.

Btw, that's a colon, not a semi-colon ;)