It has several ways but I think the best will be an array with explode:
$frase = "O + rato + roeu + a + roupa + do + rei + de + roma";
$palavras = explode('+', $frase);
Now have an array, doing print_r($palavras);
:
Array ( [0] => O [1] => rat [2] => roeu [3] => a [4] => clothing [5] => do [6] => rei [7] => de [8] => roma )
You can access any of the words via their index within this array, base 0:
$palavras[0] = ' O ';
$palavras[1] = ' rato ';
...
If you want to put them back together in a sentence without the plus signs just make a implode:
$frase = implode('', $palavras); // não coloco espaço no primeiro argumento pois este já existe em cada uma das palavras do nosso array
And it stays:
The rat gnawed on the clothes of the king of rome
However doing what you asked, using the for cycle and having a dynamic variable for each word (in this context do not recommend) can do:
$frase = "O + rato + roeu + a + roupa + do + rei + de + roma";
$palavras = explode('+', $frase);
for($i = 0, $count = count($palavras); $i < $count; $i++) {
$varNum = $i+1;
${'var' . $varNum} = $palavras[$i];
}
echo $var1; // O
...
echo $var9; // roma
Perfect Andrei, thank you very much.
– abduzeedo
Tranquil @abduzeedo... be happy =)
– Andrei Coelho
Don’t forget to approve the reply @abduzeedo. Hug
– Andrei Coelho