preg_replace all but digits and semicolon :

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
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

preg_replace all but digits and semicolon :

Post 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?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: preg_replace all but digits and semicolon :

Post by jackpf »

Code: Select all

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

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