-3

正規表現を使用して質問のグループから質問と選択肢と回答を抽出する方法

例 :

1. An FMS system, besides controlling navigation, thrust and auto-nav,
also provides:
a) take-off and landing warnings
* b) dedicated status and warnings
c) GPWS warnings

2. EADI sky and ground display is provided by:
a) synthetic TV signals
* b) raster scan
c) stroke pulse

私はこのような出力が必要です:

$question[0] = "An FMS system, besides controlling navigation, thrust and auto-nav,also provides:";
$choice1[0] = "take-off and landing warnings";
$choice2[0] = "dedicated status and warnings";
$choice3[0] = "GPWS warnings";

$question[1] = "EADI sky and ground display is provided by:";
$choice1[1] = "synthetic TV signals";
$choice2[1] = "raster scan";
$choice3[1] = "stroke pulse";

記号は、*どのパターンを書くべきかを答えることを意味します

4

4 に答える 4

3
$string = '1. An FMS system, besides controlling navigation, thrust and auto-nav,
also provides:
a) take-off and landing warnings
* b) dedicated status and warnings
c) GPWS warnings

2. EADI sky and ground display is provided by:
a) synthetic TV signals
* b) raster scan
c) stroke pulse';

$qa = array();
$string = explode("\n\n", $string);
foreach ($string as $set)
{
    $set = preg_split('/(\:?\s(\* )?[abc]\) )/', $set);
    $qa[] = array('question' => ltrim(strstr($set[0], ' ')), 
        'choice1' => $set[1],  'choice2' => $set[2],  'choice3' => $set[3]);
}

print_r($qa);

出力:

Array
(
    [0] => Array
        (
            [question] => An FMS system, besides controlling navigation, thrust and auto-nav,
also provides
            [choice1] => take-off and landing warnings
            [choice2] => dedicated status and warnings
            [choice3] => GPWS warnings
        )

    [1] => Array
        (
            [question] => EADI sky and ground display is provided by
            [choice1] => synthetic TV signals
            [choice2] => raster scan
            [choice3] => stroke pulse
        )

)
于 2013-01-02T00:40:24.540 に答える
0
  1. 空の行を爆発させて、各質問と回答を取得します。
  2. それらのそれぞれは、質問と回答に分離するために、最大 2 つの部分である と newline で展開され:ます
  3. 回答を取得するには、改行で回答を展開します。

終わり。使用される PHP 関数: explode、マニュアルには、この回答にこれ以上の例を含めることができなかったより多くの例が付属しています。

于 2013-01-02T00:23:35.230 に答える
0

次のように、答えを配列に入れることができます。

 $text='1. An FMS system, besides controlling navigation, thrust and auto-nav,
      also provides:
      a) take-off and landing warnings
      * b) dedicated status and warnings
      c) GPWS warnings

      2. EADI sky and ground display is provided by:
      a) synthetic TV signals
      * b) raster scan
      c) stroke pulse';



        $arr=explode("\n",$text);
        $correctanswer=array();
        $replacements=array('*','a)','b)','c)');
        $questions=array();
        $answers=array();
        $x=0;
        foreach($arr as $ar){
       if(preg_match('#[0-9]#',$ar)){
       $questions[$x][]=preg_replace('#[0-9]\\.#','',$ar);
       $x++;
         }

       if(preg_match('#[*]#',$ar)){
        $correctanswer[$x-1][]=str_replace($replacements,'',$ar);
         }
        $answers[$x-1][]=str_replace($replacements,'',$ar);
          }
      print_r($questions);
       echo '<br/>';
      print_r($correctanswer);

版画:

    Array
       (
       [0] => Array
           (
             [0] =>  An FMS system, besides controlling navigation, thrust and auto-     nav,
             )

      [1] => Array
            (
                [0] =>  EADI sky and ground display is provided by:
              )

            )
        Array
           (
            [0] => Array
                  (
                       [0] =>   dedicated status and warnings
                  )

            [1] => Array
                  (
                   [0] =>   raster scan
                   )

            )
于 2013-01-02T00:32:46.143 に答える
0
// Set up some variables
$question = $choice1 = $choice2 = $choice3 = $correct = array();

$string = '1. An FMS system, besides controlling navigation, thrust and auto-nav,
also provides:
a) take-off and landing warnings
* b) dedicated status and warnings
c) GPWS warnings

2. EADI sky and ground display is provided by:
a) synthetic TV signals
* b) raster scan
c) stroke pulse';

// Break question string into an array of questions with answers
$data = explode("\r\n\r\n", $string);

// Iterate through each question with answers
foreach ($data AS $i => $tmp)
{
    // Extract question and answers
    preg_match('/\d+\. (.*):\r\n(\*?)\ ?[abc]\) (.*)\r\n(\*?)\ ?[abc]\)\ (.*)\r\n(\*?)\ ?[abc]\)\ (.*)/s', $tmp, $matches);

    // Assign matches to variables
    $question[$i] = $matches[1];
    $choice1[$i] = $matches[3];
    $choice2[$i] = $matches[5];
    $choice3[$i] = $matches[7];

    // Figure out which answer is correct
    if ($matches[2] == '*')
    {
        $correct[$i] = 1;
    }
    else if ($matches[4] == '*')
    {
        $correct[$i] = 2;
    }
    else if ($matches[6] == '*')
    {
        $correct[$i] = 3;
    }
}

var_dump($question, $choice1, $choice2, $choice3, $correct);

出力:

// $question
array(2) {
  [0]=>
  string(82) "An FMS system, besides controlling navigation, thrust and auto-nav,
also provides"
  [1]=>
  string(42) "EADI sky and ground display is provided by"
}
// $choice1
array(2) {
  [0]=>
  string(29) "take-off and landing warnings"
  [1]=>
  string(20) "synthetic TV signals"
}
// $choice2
array(2) {
  [0]=>
  string(29) "dedicated status and warnings"
  [1]=>
  string(11) "raster scan"
}
// $choice3
array(2) {
  [0]=>
  string(13) "GPWS warnings"
  [1]=>
  string(12) "stroke pulse"
}
// $correct
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(2)
}
于 2013-01-02T00:38:09.607 に答える