0

そのため、この配列にアンダースコアが含まれているかどうかを確認しようとしています。これを行うために正しい関数を使用しているかどうかはわかりません。任意の入力をいただければ幸いです。

さらに詳しい情報ですが、配列にアンダースコアが含まれている場合は、以下のコードを実行してください。このコードは分離して、必要な属性を提供してくれます。また、Sがあるかどうかを確認してから、コードを実行します。これらはすべてクエリに出力され、最後にクエリされます。

    if (count($h)==3){
           if (strpos($h[2], '_') !== false)  // test to see if this is a weird quiestion ID with an underscore
               {
                    if (strpos($h[2], 'S') !== false)
                    {
                     // it has an S
                     $underscoreLocation = strpos($h[2], '_');
                     $parent = substr($h[2], 0, $underscoreLocation - 6); // start at beginning and go to S
                     $title = substr($h[2], $underscoreLocation - 5, 5);
                     $questions = "select question from lime_questions where sid =".$h[0]." and gid =".$h[1]." and parent_qid =".$parent." and title =".$title.";";
                    }
                    else
                    {
                     // there is no S
                     $underscoreLocation = strpos($h[2], '_');
                     $parent = substr($h[2], 0, $underscoreLocation - 2);
                     $title = substr($h[2], $underscoreLocation - 1, 1);
                     $questions = "select question from lime_questions where sid =".$h[0]." and gid =".$h[1]." and parent_qid =".$parent." and title =".$title.";";
                    }    
               }

           else
           {
            $questions = "select question from lime_questions where sid =".$h[0]." and gid =".$h[1]." and qid =".$h[2].";";
           }
4

1 に答える 1

1

strpos()は、文字列内に部分文字列が存在するかどうかを確認するときに使用するのに適した関数であるため、基本的な前提条件は問題ありません。

strpos()に送信する干し草の山(つまり$ h [2])は文字列ですよね?あなたはあなたの質問で、配列にアンダースコアが含まれているかどうかをチェックしていると言いますが、コードは単一の配列アイテムにアンダースコアが含まれているかどうかをチェックするだけです-これらは2つの非常に異なるものです。

$ h[2]が$h配列内の単なる文字列ではなくサブ配列である場合は、サブ配列を反復処理して各項目をチェックする必要があります。

それで:

  for ($x=0; $x<count($h[2]); $x++) {
     if (strpos($h[2][$x], "_")!==false) {
         if (strpos($h[2][$x], 'S') !== false) {
            // Run code
         } else { 
            // Run code
         }
     }
  }

$ h [2]が単なる文字列である場合、あなたが持っているものは問題ないはずです。


更新:追加してみてください

print($h[2][$x].' - '.strpos($h[2][$x], ''));

前の行に

print ($h[2][$x].' - '.strpos($h[2][$x], '')); 

これにより、問題が何であるかがわかります。


アップデート:

実行したコードに基づいて、私が思っていたものとは大きく異なります。まず、返される$h配列のすべてに3つの項目があるわけではありません。次に、$ h2は攪拌であり、サブアレイではありません。

新しいコードは次のとおりです。

  if (count($h)==3) {
     print($h2.' | ');
     if (strpos($h[2], "_")!==false) {
         print(' underscore was found | ');
         if (strpos($h[2], 'S') !== false) {
            // Run code
         } else { 
            // Run code
         }
     }
  } else {
     // array does not represent a question
  }

また、すべての$ h [2][$x]を$h[2]だけに戻す必要があります。どうなるか教えてください。

于 2012-11-28T21:29:12.453 に答える