realWasabi Posted December 11, 2013 Share Posted December 11, 2013 I'm looking for a way to find and capture the first number and all text after it in a string. Examples: I have the following entries: some text 23 text sometext 21 sometext some text 120 text I want the script to capture the number, and everything after it, and put it into a variable. At the same time, the captured part should be removed from the original string. So the above examples whould be: some text 23 text >> some text | 23 text sometext 21 >> sometext | 21 sometext some text 120 text >> sometext some text | 120 text How can this be done? I was suggested to use preg_match, but i can't figure out how to write that to do what I want. Quote Link to comment https://forums.phpfreaks.com/topic/284697-how-to-capture-the-first-number-along-with-all-text-after-it-in-string/ Share on other sites More sharing options...
requinix Posted December 11, 2013 Share Posted December 11, 2013 Regular expressions? Take a walk on the wild side! $len = strcspn($string, "1234567890"); $left = substr($string, 0, $len); $right = substr($string, $len);Consider trim()ing the two values too. Quote Link to comment https://forums.phpfreaks.com/topic/284697-how-to-capture-the-first-number-along-with-all-text-after-it-in-string/#findComment-1462028 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.