2

This is my sample string (this one has five words; in practice, there may be more):

$str = "I want to filter it";

Output that I want:

$output[1] = array("I","want","to","filter","it");
$output[2] = array("I want","want to","to filter","filter it");
$output[3] = array("I want to","want to filter","to filter it");
$output[4] = array("I want to filter","want to filter it");
$output[5] = array("I want to filter it");  

What I am trying:

$text = trim($str);
$text_exp = explode(' ',$str);
$len = count($text_exp);
$output[$len][] = $text;  // last element
$output[1] = $text_exp;  // first element

This gives me the first and the last arrays. How can I get all the middle arrays?

3 Answers 3

2

more generic solution that works with any length word:

$output = array();

$terms = explode(' ',$str);
for ($i = 1; $i <= count($terms); $i++ )
{
    $round_output = array();
    for ($j = 0; $j <= count($terms) - $i; $j++)
    {
        $round_output[] = implode(" ", array_slice($terms, $j, $i));
    }
    $output[] = $round_output;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do that easily with regular expressions that give you the most flexibility. See below for the way that supports dynamic string length and multiple white characters between words and also does only one loop which should make it more efficient for long strings..

<?php
$str = "I want to filter it";
$count = count(preg_split("/\s+/", $str));
$results = [];

for($i = 1; $i <= $count; ++$i) {
    $expr = '/(?=((^|\s+)(' . implode('\s+', array_fill(0, $i, '[^\s]+')) . ')($|\s+)))/';
    preg_match_all($expr, $str, $matches);
    $results[$i] = $matches[3];
}

print_r($results);

Comments

0

You can use a single for loop and if conditions to do

$str = "I want to filter it"; 

$text = trim($str);
$text_exp = explode(' ',$str);
$len = count($text_exp);

$output1=$text_exp;
$output2=array();
$output3=array();
$output4=array();
$output5=array();
for($i=0;$i<count($text_exp);$i++)
{
    if($i+1<count($text_exp) && $text_exp[$i+1]!='')
    {
        $output2[]=$text_exp[$i].' '.$text_exp[$i+1];
    }

    if($i+2<count($text_exp) && $text_exp[$i+2]!='')
    {
        $output3[]=$text_exp[$i].' '.$text_exp[$i+1].' '.$text_exp[$i+2];
    }
    if($i+3<count($text_exp) && $text_exp[$i+3]!='')
    {
        $output4[]=$text_exp[$i].' '.$text_exp[$i+1].' '.$text_exp[$i+2].' '.$text_exp[$i+3];
    }
    if($i+4<count($text_exp) && $text_exp[$i+4]!='')
    {
        $output5[]=$text_exp[$i].' '.$text_exp[$i+1].' '.$text_exp[$i+2].' '.$text_exp[$i+3].' '.$text_exp[$i+4];
    }

}

3 Comments

Note that it will work for five words string only. @Adam
Ok. you use the same with the output1, output2, etc.. as array
I suggest you to wait for better answers @AdamSmith

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.