Not sure how to explain. Let's use an example. Say I want to split the sentence
"Today is a great day."
into
today
today is
today is a
today is a great
today is a great day
is
is a
is a great
is a great day
a
a great
a great day
great
great day
day
The idea is to get all the sequential combination in a sentence.
I have been thinking what's the best way to do it in PHP. Any idea is welcome.
Here's an example:
$sentence = 'Today is a great day.';
// Only leave "word" characters and whitespace
$sentence = preg_replace('/[^\w\s]+/', '', strtolower($sentence));
// Tokenize
$tokens = explode(' ', $sentence);
for($i = 0; $i < count($tokens); $i++) {
for($j = 1; $j <= count($tokens) - $i; $j++) {
echo implode(' ', array_slice($tokens, $i, $j)) . "<br />";
}
}
Output:
today
today is
today is a
today is a great
today is a great day
is
is a
is a great
is a great day
a
a great
a great day
great
great day
day
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With