Natural Words Combination From Phrase/String - php script
Get all natural word combination from phrase:
<?php
function natural_combi_words($phrase)
{
$arw = array();
$words = explode(’ ‘,trim($phrase));
$nw = count ($words);
for ($i=0; $i<$nw; $i++)
{
$k =”;
$cnt = 0;
for ($j=$i; $j<($nw); $j++)
{
$k .= $words[$j].’ ‘;
$arw[$cnt][] = trim($k);
$cnt++;
}
}
return $arw;
}function natural_combi_words2($phrase)
{
$combi_source = natural_combi_words($phrase);
$combi_words = $combi_source[0];
$nw = count ($combi_source);
for ($i=1; $i<$nw; $i++)
{
$combi_words = array_merge($combi_words, $combi_source[$i]);
}
return $combi_words;
}$phrase = “I have a dream that one day”;
print_r(natural_combi_words($phrase);
print_r(natural_combi_words2($phrase));?>
if phrase is: I have a dream that one day , function natural_combi_words2() returns:
Array
(
[0] => I
[1] => have
[2] => a
[3] => dream
[4] => that
[5] => one
[6] => day
[7] => I have
[8] => have a
[9] => a dream
[10] => dream that
[11] => that one
[12] => one day
[13] => I have a
[14] => have a dream
[15] => a dream that
[16] => dream that one
[17] => that one day
[18] => I have a dream
[19] => have a dream that
[20] => a dream that one
[21] => dream that one day
[22] => I have a dream that
[23] => have a dream that one
[24] => a dream that one day
[25] => I have a dream that one
[26] => have a dream that one day
[27] => I have a dream that one day
)
and function natural_combi_words() returns multi-dimensional arrays like this:
Array
(
[0] => Array
(
[0] => I
[1] => have
[2] => a
[3] => dream
[4] => that
[5] => one
[6] => day
)[1] => Array
(
[0] => I have
[1] => have a
[2] => a dream
[3] => dream that
[4] => that one
[5] => one day
)[2] => Array
(
[0] => I have a
[1] => have a dream
[2] => a dream that
[3] => dream that one
[4] => that one day
)[3] => Array
(
[0] => I have a dream
[1] => have a dream that
[2] => a dream that one
[3] => dream that one day
)[4] => Array
(
[0] => I have a dream that
[1] => have a dream that one
[2] => a dream that one day
)[5] => Array
(
[0] => I have a dream that one
[1] => have a dream that one day
)[6] => Array
(
[0] => I have a dream that one day
))
(ByREV)















is more easy
… string / word permutation !
Great Article! But how to create all possible combinations, not only natural words?