kickstart Posted March 25, 2009 Share Posted March 25, 2009 Hi Basically if I have a string I want to break it down into several substrings of a certain number of words (which will vary), but with the results overlapping. For example, the string "The quick brown fox jumped over the lazy dog", and I want that split up into strings of wordssuch that I get back:- "The quick brown fox" "quick brown fox jumped" "brown fox jumped over" "fox jumped over the" "jumped over the lazy" "over the lazy dog" I can think of ways to manually do this, but just wondering in anyone knows of a single step way to do this using regular expressions (or similar). All the best Keith Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted March 25, 2009 Share Posted March 25, 2009 best way would be to write your own functions $response=myOwnSplit($string, $count, $interval); $arrays function myOwnSplit($string,$count,$interval) { $array=split(" ",$string); $counter=floor(count($array)/$count); for($a=0;$a<$counter;$a++) { $response=""; for($b=1;$b<=$interval;$b++) { $response.=$array[$a][$b]; } $responses[]=$response; } return $response; } that should get you pretty damn close. You might have to tweak it a bit. That's just off the top of my head. Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 25, 2009 Author Share Posted March 25, 2009 Hi Thanks but unfortunately that is what I am trying to avoid doing, as I suspect calling functions repeatedly would be rather inefficient compared to (if one exists) some kind of regular expression. All the best Keith Quote Link to comment Share on other sites More sharing options...
hastishah Posted March 25, 2009 Share Posted March 25, 2009 Hi Just Check out the Code below. I think you can get an idea from this. <?php $temp = "The quick brown fox quick brown fox jumped brown fox jumped over fox jumped over the jumped over the lazy over the lazy dog"; echo $temp."<br />"; $temp = explode(" ",$temp); $temp = array_unique($temp); $temp = implode(" ",$temp); echo $temp; ?> Quote Link to comment 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.