ghostwalkz Posted May 22, 2009 Share Posted May 22, 2009 Hi all, I need to extract every second word in a string: eg: $foo = "one two three four five six" I need $bar to only consist of every other word like this: $bar = "one three five" How might I do this, also any code examples would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/159256-solved-extract-every-second-word-in-a-string/ Share on other sites More sharing options...
Yeodan Posted May 22, 2009 Share Posted May 22, 2009 http://be.php.net/manual/en/control-structures.for.php http://be.php.net/manual/en/control-structures.if.php http://be.php.net/substr http://be.php.net/strlen if you understand those you know how to do it if not, loop trough every character in the string if the character is a " " (space) you know the next word begins add a counter that counts wich word you're at add more counters to know where the word starts and ends add the words you need to the new string Quote Link to comment https://forums.phpfreaks.com/topic/159256-solved-extract-every-second-word-in-a-string/#findComment-839923 Share on other sites More sharing options...
Axeia Posted May 22, 2009 Share Posted May 22, 2009 $foo = "one two three four five six"; $arr = explode( ' ', $foo ); for( $i = 0; $i < count( $arr ); $i++ ) if( $arr[i] % 2 !== 0 ) echo $arr[i].' '; Untested and knowing myself it probably has some typo in it. Quote Link to comment https://forums.phpfreaks.com/topic/159256-solved-extract-every-second-word-in-a-string/#findComment-839928 Share on other sites More sharing options...
Ken2k7 Posted May 22, 2009 Share Posted May 22, 2009 Axeia: 1. i is undefined in $arr. 2. $arr[$i] % 2 will always be zero, so the condition will never be true. Quote Link to comment https://forums.phpfreaks.com/topic/159256-solved-extract-every-second-word-in-a-string/#findComment-840100 Share on other sites More sharing options...
Axeia Posted May 22, 2009 Share Posted May 22, 2009 $foo = "one two three four five six"; $arr = explode( ' ', $foo ); for( $i = 0; $i < count( $arr ); $i++ ) if( $i % 2 == 0 ) echo $arr[$i].' '; I keep messing up variables lately, working in PHP, javascript and java all at once makes you mess up a lot of things.. not to speak of my own mistakes, code above is tested and actually works Quote Link to comment https://forums.phpfreaks.com/topic/159256-solved-extract-every-second-word-in-a-string/#findComment-840102 Share on other sites More sharing options...
Ken2k7 Posted May 22, 2009 Share Posted May 22, 2009 And here's my version: (just thought I add some new flavor into this) $foo = 'one two three four five'; $tok = $bar = strtok($foo, ' '); while (strtok(' ') !== false && ($next = strtok(' ')) !== false) $bar .= ' ' . $next; var_dump($bar); Quote Link to comment https://forums.phpfreaks.com/topic/159256-solved-extract-every-second-word-in-a-string/#findComment-840117 Share on other sites More sharing options...
ghostwalkz Posted May 22, 2009 Author Share Posted May 22, 2009 And here's my version: (just thought I add some new flavor into this) $foo = 'one two three four five'; $tok = $bar = strtok($foo, ' '); while (strtok(' ') !== false && ($next = strtok(' ')) !== false) $bar .= ' ' . $next; var_dump($bar); Now thats what I am talking about!!! TY!!! Fixed! Quote Link to comment https://forums.phpfreaks.com/topic/159256-solved-extract-every-second-word-in-a-string/#findComment-840289 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.