-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. 每一个都在:换行符上展开,最多两部分,分成问题和答案。
  3. 在换行符处展开答案以获得答案。

完毕。PHP function used: 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 回答