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
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.

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.