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 Link to comment https://forums.phpfreaks.com/topic/151041-splitting-a-string-with-a-difference/ 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. Link to comment https://forums.phpfreaks.com/topic/151041-splitting-a-string-with-a-difference/#findComment-793504 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 Link to comment https://forums.phpfreaks.com/topic/151041-splitting-a-string-with-a-difference/#findComment-793508 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; ?> Link to comment https://forums.phpfreaks.com/topic/151041-splitting-a-string-with-a-difference/#findComment-793537 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.