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? Quote 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 Quote 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; Quote 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 Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.