MrCat Posted September 30, 2011 Share Posted September 30, 2011 Is there an easy way to find a numeric value in a string if I don't know what the string will contain? For example I want '52' out of: mystring52 Hopefully there is already a simple function to do this? I'm thinking there may not be as the string having more than one group of numbers may confuse matters. I'm just dealing with abcdef123 or similar in this case though. Quote Link to comment https://forums.phpfreaks.com/topic/248160-find-numeric-value-in-string/ Share on other sites More sharing options...
Buddski Posted September 30, 2011 Share Posted September 30, 2011 You could use a regular expression. I am horrible when it comes to regex but this at least works $str = 'abc343def123'; preg_match_all('/[^a-zA-Z]{1,}/',$str,$matches); echo '<pre>' , print_r($matches,true) , '</pre>' /* Results in Array ( [0] => Array ( [0] => 343 [1] => 123 ) )*/ Edit: even better preg_match_all('/\d{1,}/',$str,$matches); Quote Link to comment https://forums.phpfreaks.com/topic/248160-find-numeric-value-in-string/#findComment-1274309 Share on other sites More sharing options...
MrCat Posted September 30, 2011 Author Share Posted September 30, 2011 Thanks for that. It would be nice if something like intval just worked. I always worry about time/processing penalties when using something unnecessarily complicated like preg_match! Quote Link to comment https://forums.phpfreaks.com/topic/248160-find-numeric-value-in-string/#findComment-1274496 Share on other sites More sharing options...
ManiacDan Posted September 30, 2011 Share Posted September 30, 2011 It would be nice if something like intval just workedintval does work. If you give it "potato123" intval says "that's not a number, so let's say zero." What happens if your string is "ab12cd34." Do you want the number 1,234 or do you want two numbers? Quote Link to comment https://forums.phpfreaks.com/topic/248160-find-numeric-value-in-string/#findComment-1274506 Share on other sites More sharing options...
MrCat Posted September 30, 2011 Author Share Posted September 30, 2011 Yeah I know what you mean. What I'm wanting to do is pass a 'get' variable like ?page=thisone26 I can always do ?page=thisone&value=26 or something like ?page=thisone.26 and look for the separator. Just thought my original layout was the nicest but it's looking like it's not worth the trouble! Quote Link to comment https://forums.phpfreaks.com/topic/248160-find-numeric-value-in-string/#findComment-1274515 Share on other sites More sharing options...
xyph Posted September 30, 2011 Share Posted September 30, 2011 why not use a separator and explode? Quote Link to comment https://forums.phpfreaks.com/topic/248160-find-numeric-value-in-string/#findComment-1274517 Share on other sites More sharing options...
requinix Posted September 30, 2011 Share Posted September 30, 2011 "thisone26" is just in that form to look pretty I take it. There are two actual pieces of data, but because one is letters and the other is numbers you can combine them unambiguously. Get the best of both worlds by using URL rewriting. You could use /page/thisone26 and RewriteRule ^page/(\D+)(\d+)$ index.php?page=$1&value=$2 [L] Quote Link to comment https://forums.phpfreaks.com/topic/248160-find-numeric-value-in-string/#findComment-1274522 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.