playaz Posted May 15, 2009 Share Posted May 15, 2009 Hi guys, I am a total newbie at regex - I require an expression that will remove all letters from a string eg.. Below is an example of original string, and the result I would need in the $newString variable - what expression would be required to do this? $string = '472PP'; $newString = '472'; Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/158253-expression-to-return-numbers-only/ Share on other sites More sharing options...
Adam Posted May 15, 2009 Share Posted May 15, 2009 With regex you could do it like this: $number = preg_replace('/[^0-9]*/', '', $str); Quote Link to comment https://forums.phpfreaks.com/topic/158253-expression-to-return-numbers-only/#findComment-834674 Share on other sites More sharing options...
playaz Posted May 15, 2009 Author Share Posted May 15, 2009 Thanks Adam!! thats superb :-) Quote Link to comment https://forums.phpfreaks.com/topic/158253-expression-to-return-numbers-only/#findComment-834683 Share on other sites More sharing options...
.josh Posted May 15, 2009 Share Posted May 15, 2009 you don't technically need that * in there. Quote Link to comment https://forums.phpfreaks.com/topic/158253-expression-to-return-numbers-only/#findComment-834707 Share on other sites More sharing options...
Adam Posted May 18, 2009 Share Posted May 18, 2009 Ha yerrrr I know! Just like the stars! Quote Link to comment https://forums.phpfreaks.com/topic/158253-expression-to-return-numbers-only/#findComment-836263 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.