Jump to content

Splitting a string - with a difference.


kickstart

Recommended Posts

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

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.

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.