dadamssg Posted July 21, 2009 Share Posted July 21, 2009 I'm attaching images to events. And im going to name the events the timestamp that they end at and then attach the creators login name. So an example would be 324559823759admin.png I know that "timestamp" probably isn't legit but...i need a function that will grab all those beginning numbers up until it hits a letter...so the function would return "324559823759" in this case if "324559823759admin.png" was inputted. I have no clue how to do this...does anybody??? Quote Link to comment https://forums.phpfreaks.com/topic/166873-solved-function-to-grab-number-out-of-a-string/ Share on other sites More sharing options...
nrg_alpha Posted July 21, 2009 Share Posted July 21, 2009 There are numerous ways to skin this cat I'd wager.. two possible solutions could be: Example 1: function grabNumbers($str){ preg_match('#^\d+#', $str, $match); return $match[0]; } $file = '324559823759admin.png'; echo grabNumbers($file); // Output: 324559823759 Exmaple 2 (using a possible non-regex solution): function grabNumbers($str){ list($number,$string) = sscanf($str, "%d%s"); // if using a 64-bit system simply return $number - otherwise, return str_replace($string, '', $str); // we remove string from filename instead } $file = '324559823759admin.png'; echo grabNumbers($file); // Output: 324559823759 Quote Link to comment https://forums.phpfreaks.com/topic/166873-solved-function-to-grab-number-out-of-a-string/#findComment-879896 Share on other sites More sharing options...
dadamssg Posted July 22, 2009 Author Share Posted July 22, 2009 ok...what about if i had an image with this name 1248840000Admin614.png (notice the numbers after the Admin) would both of those just return the front number? cause thats what i need if so...you're awesome Quote Link to comment https://forums.phpfreaks.com/topic/166873-solved-function-to-grab-number-out-of-a-string/#findComment-879900 Share on other sites More sharing options...
dadamssg Posted July 22, 2009 Author Share Posted July 22, 2009 i just tried both of these with no luck... function grabNumbers($str){ preg_match('#^\d+#', $str, $match); return $match[0]; } $file = '324559823759admin235.png'; echo grabNumbers($file); // Output: 324559823759 function grabNumberss($str){ list($number,$string) = sscanf($str, "%d%s"); // if using a 64-bit system simply return $number - otherwise, return str_replace($string, '', $str); // we remove string from filename instead } $file = '324559823759admin3465.png'; echo grabNumberss($file); // Output: 324559823759 Quote Link to comment https://forums.phpfreaks.com/topic/166873-solved-function-to-grab-number-out-of-a-string/#findComment-879901 Share on other sites More sharing options...
dadamssg Posted July 22, 2009 Author Share Posted July 22, 2009 wait....just kidding...uploading the file to the freaking wrong directory...works perfect, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/166873-solved-function-to-grab-number-out-of-a-string/#findComment-879906 Share on other sites More sharing options...
nrg_alpha Posted July 22, 2009 Share Posted July 22, 2009 All's good. Quote Link to comment https://forums.phpfreaks.com/topic/166873-solved-function-to-grab-number-out-of-a-string/#findComment-879911 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.