sKunKbad Posted June 15, 2009 Share Posted June 15, 2009 I'm looking for a function to remove all characters that aren't numbers from a string. Does php have anything built in, or do I have to come up with something on my own? Link to comment https://forums.phpfreaks.com/topic/162199-solved-strip-any-chars-that-arent-numbers-from-string/ Share on other sites More sharing options...
rhodesa Posted June 15, 2009 Share Posted June 15, 2009 this should do the trick $string = 'abc123'; $new = preg_replace('/[^\d]+/',$string); echo $new Link to comment https://forums.phpfreaks.com/topic/162199-solved-strip-any-chars-that-arent-numbers-from-string/#findComment-855968 Share on other sites More sharing options...
Ken2k7 Posted June 15, 2009 Share Posted June 15, 2009 rhodesa, you're missing the second argument in preg_replace. Also, the + is not needed. Lastly, semi-colon in the echo statement is missing. <?php $string = 'abc123'; $new = preg_replace('/[^\d]/', '', $string); echo $new; Link to comment https://forums.phpfreaks.com/topic/162199-solved-strip-any-chars-that-arent-numbers-from-string/#findComment-855994 Share on other sites More sharing options...
rhodesa Posted June 15, 2009 Share Posted June 15, 2009 thanks @Ken2k7 Link to comment https://forums.phpfreaks.com/topic/162199-solved-strip-any-chars-that-arent-numbers-from-string/#findComment-856142 Share on other sites More sharing options...
nrg_alpha Posted June 16, 2009 Share Posted June 16, 2009 Alternatively, one could use \D instead of the [^\d] negated character class. Link to comment https://forums.phpfreaks.com/topic/162199-solved-strip-any-chars-that-arent-numbers-from-string/#findComment-857313 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.