2013-01-02 85 views
-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"; 

*號表示回答 什麼模式應該我寫 非常感謝

+2

我覺得這樣做可以通過按行分割和檢查前幾個字符來更好地完成。 –

+0

我想單獨選擇問題並將它們保存到數據庫中 –

+2

那麼,到目前爲止您嘗試過了什麼?你在哪裏碰到路障?否則,您可以聘請自由職業者爲您提供一些代碼,並且很樂意應用您稍後想出的任何更改。 – hakre

回答

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 
     ) 

) 
+1

很整齊,乾淨。 –

0
  1. 在空行上爆炸以獲得每個問題+答案。
  2. 每個爆炸的:換行,最多兩部分,分爲問題和答案。
  3. 在換行符處分解答案以獲取答案。

完成。使用PHP函數:explode,手冊附帶更多的例子,我不能在這裏回答更好的例子。

+0

我想刪除「a)b)c)」 –

+0

我不會阻止你刪除它。獲得所有答案後很容易,只需刪除即可。 – hakre

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 
        ) 

      ) 
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) 
}