adamyork Posted December 23, 2010 Share Posted December 23, 2010 I have a text field that contains the following (example text used): 1 2 3 4 5 6 As you can see the first few are split by a single space, with the last few numbers split by a double space. My aim is to put each of these numbers into a table in a MYSQL database. But I'm having trouble breaking them up. Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/ Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 one way to get them into an array: $str = "1 2 3 4 5 6"; $str = preg_replace('/ +/',' ',$str); $vals = explode(" ",$str); print_r($vals); Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150825 Share on other sites More sharing options...
Maq Posted December 23, 2010 Share Posted December 23, 2010 How big is this string going to be? If it's relatively small you could use some regex: $string = "1 2 3 4 5 6"; $string = preg_replace('/\s+/', ' ', $string); $arr = explode(' ', $string); print_r($arr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150826 Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 hm, that looks familiar. :-) Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150828 Share on other sites More sharing options...
Maq Posted December 23, 2010 Share Posted December 23, 2010 hm, that looks familiar. :-) Nah, they're totally different. I mean, you used double quotes and I used all single ;P Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150830 Share on other sites More sharing options...
adamyork Posted December 23, 2010 Author Share Posted December 23, 2010 Ok, I'm maybe pushing it now (!), but is there any way I can get it to skip spaces in-between text, and just separate in-between numbers? e.g. Hello there 1 2 3 4 5 [1] Hello there [2] 1 [3] 2 [4] 3 [5] 4 [6] 5 Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150860 Share on other sites More sharing options...
bals28mjk Posted December 23, 2010 Share Posted December 23, 2010 Untested, but try: $string = "1 2 3 4 5 6"; preg_match_all('/\s+/',$string, $matches); print_r($matches); http://www.php.net/manual/en/ref.pcre.php Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150870 Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 actually, no that last one wouldn't work... Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150872 Share on other sites More sharing options...
Maq Posted December 23, 2010 Share Posted December 23, 2010 Please read all the posts, not just the first one. We have already solved the first issue, the OP has presented a second one. (BTW, your regex only matches/returns spaces) Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150875 Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 updated code for the latest question, first removing anything that isn't a space or a number, then replacing double-spaces with single spaces, then explode to array of numbers. $str = "Hello there 1 2 3 4 5"; $str = preg_replace('/[^\s\d]/','',$str); $str = preg_replace('/\s+/',' ',$str); $vals = explode(" ",$str); print_r($vals); Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150878 Share on other sites More sharing options...
adamyork Posted December 23, 2010 Author Share Posted December 23, 2010 updated code for the latest question, first removing anything that isn't a space or a number, then replacing double-spaces with single spaces, then explode to array of numbers. $str = "Hello there 1 2 3 4 5"; $str = preg_replace('/[^\s\d]/','',$str); $str = preg_replace('/\s+/',' ',$str); $vals = explode(" ",$str); print_r($vals); This helps in getting rid of the words all together. But I was hoping to keep both the words in the array, but simply put them as one single result... Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150884 Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 i guess i would perform similar functions on the start string twice: once to isolate numbers and once to isolate letters. then make the letters (words) the first element of the array. or move on to preg_match(), using regex to separate letters at the beginning and numbers at the end... Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150886 Share on other sites More sharing options...
bals28mjk Posted December 23, 2010 Share Posted December 23, 2010 Please read all the posts, not just the first one. We have already solved the first issue, the OP has presented a second one. (BTW, your regex only matches/returns spaces) Good catch. Perhaps a \s*\d+\s* would be a more efficient regex. *Wipes off rust* haven't been coding in a long while. I think that one searches for zero or more spaces, followed by at least one digit, followed by zero or more spaces. I changed the spacing to allow for zero spaces to take into account spaces at the beginning and end of the string. Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150895 Share on other sites More sharing options...
adamyork Posted December 23, 2010 Author Share Posted December 23, 2010 Unfortunately this is all new to me, so really struggling to get what I want from it! I'm now creating a separate array for the words. But I still want it so that when there is 2 words with a space in between, the space is kept and they become one result. e.g. 1 Hello there 1 2 3 as... [0 ] Hello there rather than... [0 ] Hello [1] there Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150910 Share on other sites More sharing options...
laffin Posted December 23, 2010 Share Posted December 23, 2010 <?php $str="Hello there 1 2 3 4 5"; preg_match_all('/((?:[a-z]\w*\s?)+|(?:\d+))(?:\s+|$)/i',$str,$matches); print_r($matches); I got this for the output ( [0] => Array ( [0] => Hello there [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 ) [1] => Array ( [0] => Hello there [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/222514-splitting-text/#findComment-1150914 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.