次のコードは次の$input
ようになります。"a banana is a fruit"
$t = sscanf($input,"a %s is a %s",$child,$parent);
echo $t.",".$child.",".$parent;
そしてそれは出力します:2,banana,fruit
、これは完璧です!
ただし、$input = "a cow is an animal"
出力を取得した場合:2,cow,n
。これは理想的ではありません。
理想的には、$input = "a cow is an animal"
それが返さ1,cow,
れ、別のフォーマット文字列を使用します 。"a %s is an %s"
この入力には、を返し2,cow,animal
ます。
だから:なぜこの問題の発生a
を"a %s is a %s"
止めないのは2番目以降のスペースですか?a
正しい一致を見つけるために、その後にスペースが発生しなければならないことを明示的に宣言する方法はありますか?
うまくいけば、それは理にかなっています。助けていただければ幸いです。
編集:これが実際のコードです:
$teachArray = array("a %s is a %s","an %s is a %s","a %s is an %s","an %s is an %s");
$i=0;
$t = -1;
while( (t <= 2) && (i < count($teachArray)) ) {
$t = sscanf($input,$teachArray[i],$child,$parent);
$i++;
}
したがって、一致する文の構造が見つかるか、文のテンプレート($ teachArrayに格納されている)がなくなるまでループを続けます。