I want to get the count of characters from the following words in the string. For example, if my input is I am John then the output must be like this:
1 // count of 'I'
4 // count of 'I am'
9 // count of 'I am John'
I use the code like this in PHP for this process:
$string = 'I am John';
$words = explode(' ',$string);
$count_words = count($words);
$i =0;
while ($i<=$count_words){
$word_length =0;
$k=0;
while($k<=$i){
$word_length = strlen($words[$k-1]);
$word_length = $word_length + strlen($words[$k]);
$k++;
}
$word_length = $word_length + $i; // there is "$i" means "space"
echo $word_length.'<br/>';
$i++;
}
But it return the output like this:
1
4
8
7
Why ? Where is my error ? What does my code must be like ?
Thanks in advance!
var_dump()a lot.