2012-01-21 37 views
0

文字列内の次の単語から文字数を取得したいとします。私の入力がI am Johnある場合たとえば、その後、出力は次のようにする必要があります
whileループはPHPで正しく動作しませんか?

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 

なぜですか?私のエラーはどこですか?私のコードはどのようなものでしょうか?
ありがとうございます!

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

$count_words3ですが、理由<=4回繰り返す:

+2

Re title:いいえ、これまで使用されている最も一般的な言語の最も基本的で広く使われている言語構成の1つにバグを見つけただけではありません。 :o) – deceze

+0

ok :)あなたはどんなタイトルをお勧めしますか? :) – John

+2

これは無料です:http://codepad.viper-7.com/3R02TKしかし、真剣に、自分のコードをデバッグする方法を学んでください。そうでなければ、プログラミングで何もできません。デバッグとは、コードを段階的に調べて何が間違っているかを把握することです。 'var_dump()'をたくさん使ってください。 – deceze

答えて

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++; 
} 
?> 
1

あなたのエラーはここにあります。代わりに<を使用してください。

+0

大丈夫、ありがとう。今は1 4 8を返します。最終的になぜ「8」ですか? '8'の代わりに '9'が必要です。 – John

+1

@ジョンコードにはもう少し問題があります - 最初の繰り返しでの$ words [$ k-1]は1つにつき$ words [-1]になります。私はコードをテストし、あなたが言ったことは印刷しません。 –

+0

そう、あなたは何をお勧めしますか?私は$ words [$ k-1]のisteadを使う必要がありますか? – John

1
  1. あなたは多くの単語にループしていました。 countを使用すると、配列内の要素の数が返されます。配列が0から始まることを思い出してください。
  2. $ word_length + strlen($ words [$ k - 1]); //あなたは1を減算していたと思います。私はあなたがoffestの数を食べようとしていたと思っていますが、0から-1を引いて、最初の単語が見逃されています。

コードスニペット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++; 

} 

コードスニペットEND

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_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]で使用され

preg_match_all()このためビルトイン「フラグ」を有します無駄な空白と空の文字列は省略します)。

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

これは、スペースが続いている「ゼロ幅」という文字列に文字列を分割します。ここでは


は別の、全く異なる方法です。これは、爆発過程でスペースが失われないことを意味します。これは簡単な追加のために文字列と部分文字列の長さを維持します。

関連する問題