while循环在PHP中是否正常工作?

时间:2012-01-21 09:03:40

标签: php algorithm loops while-loop

我想从字符串中的以下单词中获取字符数。例如,如果我的输入是I am John,则输出必须如下:

1 // count of 'I'
4 // count of 'I am'
9 // count of 'I am John'

我在PHP中使用这样的代码来完成这个过程:

$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++;

}

但它会像这样返回输出:

1
4
8
7

为什么?我的错误在哪里?我的代码必须是什么样的?
提前谢谢!

4 个答案:

答案 0 :(得分:1)

您的错误在这里:

$i =0;
while ($i<=$count_words){
   //....
}

$count_words3,但由于4,您需要重复<=次。请改用<

答案 1 :(得分:1)

  1. 你正在循环使用很多单词。使用count时,它返回数组中的元素数。请记住,数组从0开始。
  2. $ word_length + strlen($ words [$ k - 1]); //你正在减去1我认为你试图迎合ffe的数量,但是你从0减去-1导致第一个单词被遗漏。
  3. CODE SNIPPET START

    //Set up the words
    
    $string = 'I am John';
    
    $words = explode(' ',$string);
    
    $count_words = count($words);
    
    //Loop through the words
    $i =0;
    
    while ($i<$count_words){
    
    $word_length =0;
    $k=0;
    
    $debugString = '';
    
    //Loop through all the previous words to the current
    while($k<= $i){
    
        //dont really need this since were adding the word length later
        //$word_length = strlen($words[$k]);
    
        //if you -1 from 0 you will get an undefined offest notice. You
        //want to look at your current word
        $word_length = $word_length + strlen($words[$k]);
    
        //A bit of debugging you can delete this once you have seen the results
        $debugString = $debugString ." ".$words[$k];
    
        $k++;
    }
    
    $word_length = $word_length + $i ; // there is "$i" means "space"
    
    //Added the debugString for debugging so remove it once you have seen the results
    echo $word_length." " .$debugString.' <br/>';
    $i++;
    
    }
    

    CODE SNIPPET END

答案 2 :(得分:1)

<?php
$string = 'I am John';
$words = explode(' ',$string);
$count_words = count($words);
$i =0;
while ($i<$count_words){
    if($i==0) {
    $wordsc[$i] = strlen($words[$i]);
    } else {
    $wordsc[$i] = strlen($words[$i])+1+$wordsc[$i-1];
    }
    echo $wordsc[$i]."<br>";
    $i++;
}
?>

答案 3 :(得分:0)

我很高兴为您提供一种完全不同的方法,以非常直接的方式生成您想要的数据。 (Demo of what is to follow

var_export(preg_match_all('/ |$/','I am John',$out,PREG_OFFSET_CAPTURE)?array_column($out[0],1):'failure');

输出:

array (
  0 => 1,
  1 => 4,
  2 => 9,
)

确定每个字增量子字符串的长度实际上与确定每个尾随空格的offset或最终字 - 完整字符串长度相同。

preg_match_all()有一个内置的&#34;标志&#34;为此:PREG_OFFSET_CAPTURE

在任何数组操作输出之前

preg_match_all()

array (
  0 => 
  array (
    0 => 
    array (
      0 => ' ',   // <-- the space after 'I' matched by ' '
      1 => 1,
    ),
    1 => 
    array (
      0 => ' ',  // <-- the space after 'am' matched by ' '
      1 => 4,
    ),
    2 => 
    array (
      0 => '',  // <-- the end of the string (after 'John') matched by '$'
      1 => 9,
    ),
  ),
)
array_column()上使用

$out[0]来仅提取偏移值(省略无用的空字符串和空字符串)。

这是另一种完全不同的方法:

array_reduce(preg_split('/(?= )/',$string),function($carry,$item){echo $carry+=strlen($item)," "; return $carry;},0);
output: 1 4 9 

这将字符串拆分为&#34;零宽度&#34;后跟空格的字符串。这意味着在爆炸过程中,空格不会丢失 - 这样可以保持字符串和子字符串的长度,以便进行简单的添加。