regex 使用preg_match_all和foreach获取检查数组

iklwldmw  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(126)

我需要一个数组来进行考试,但是使用regex和foreach后的结果不是我想要的。
我的当前结果:

Array
(
    [0] => Array
        (
            [0] => 1 Q 1.Question 1? A 0 - Answer 11
            [1] => 1 Q 1.Question 1? A 1 - Answer 12
            [2] => 1 Q 1.Question 1? A 2 - Answer 13
            [3] => 1 Q 1.Question 1? A 3 - Answer 21
            [4] => 1 Q 1.Question 1? A 4 - Answer 22
            [5] => 1 Q 1.Question 1? A 5 -  Answer 31
            [6] => 1 Q 1.Question 1? A 6 -  Answer 32
            [7] => 1 Q 1.Question 1? A 7 -  Answer 33
            [8] => 2 Q 2.Question 2? A 0 - Answer 11
            [9] => 2 Q 2.Question 2? A 1 - Answer 12
            [10] => 2 Q 2.Question 2? A 2 - Answer 13
            [11] => 2 Q 2.Question 2? A 3 - Answer 21
            [12] => 2 Q 2.Question 2? A 4 - Answer 22
            [13] => 2 Q 2.Question 2? A 5 -  Answer 31
            [14] => 2 Q 2.Question 2? A 6 -  Answer 32
            [15] => 2 Q 2.Question 2? A 7 -  Answer 33
            [16] => 3 Q 3. Question 3? A 0 - Answer 11
            [17] => 3 Q 3. Question 3? A 1 - Answer 12
            [18] => 3 Q 3. Question 3? A 2 - Answer 13
            [19] => 3 Q 3. Question 3? A 3 - Answer 21
            [20] => 3 Q 3. Question 3? A 4 - Answer 22
            [21] => 3 Q 3. Question 3? A 5 -  Answer 31
            [22] => 3 Q 3. Question 3? A 6 -  Answer 32
            [23] => 3 Q 3. Question 3? A 7 -  Answer 33
        )

)

在上面的结果中,问题和答案被重复多次的次数。
我想要的输出是:

Array
(
    [0] => Array
        (
            [0] => 1 Q 1.Question 1? A 0 - Answer 11
            [1] => 1 Q 1.Question 1? A 1 - Answer 12
            [2] => 1 Q 1.Question 1? A 2 - Answer 13
)
[1] => Array
        (
            [3] => 2 Q 2.Question 2? A 0 - Answer 21
            [4] => 2 Q 2.Question 2? A 1 - Answer 22
)
[2] => Array
        (
            [5] => 3 Q 3.Question 3? A 0 -  Answer 31
            [6] => 3 Q 3.Question 3? A 1 -  Answer 32
            [7] => 3 Q 3.Question 3? A 2 -  Answer 33
         )

)

我的当前代码(run online):

<?php
$re_question = "'<b>(.*?)</b>'si";
$re_answer = '#<input[^>]* value="([a-z]\.\)(.*?)")>#s';

$str = '<b>1.Question 1?</b><div class="data-check" style="padding-left:25px;"><input class="data-form-input" type="radio" name="q11111" value="a.)Answer 11">a.)Answer 11</div><div class="data-check" style="padding-left:25px;"><input class="data-form-input" type="radio" name="q11111" value="b.)Answer 12">b.)Answer 12</div><div class="data-check" style="padding-left:25px;"><input class="data-form-input" type="radio" name="q11111" value="b.)Answer 13">b.)Answer 13</div><br><b>2.Question 2?</b><div class="data-check" style="padding-left:25px;"><input class="data-form-input" type="radio" name="q11111" value="a.)Answer 21">a.)Answer 21</div><div class="data-check" style="padding-left:25px;"><input class="data-form-input" type="radio" name="q22" value="b.)Answer 22">b.)Answer22</div><br><b>3. Question 3?</b><div class="data-check" style="padding-left: 25px;"> <input class="data-form-input" type="radio" name="q33333" value="a.) Answer 31"> a.) Answer 31 </div><div class="data-check" style="padding-left: 25px;"> <input class="data-form-input" type="radio" name="q33333" value="b.) Answer 32"> b.) Answer 32 </div> </div><div class="data-check" style="padding-left: 25px;"> <input class="data-form-input" type="radio" name="q33333" value="b.) Answer 33"> b.) Answer 33 </div>';

$questions = "";
$answers = [];
$questions_and_answers = [];
$answer_number = 0;
$question_number = 0;
//question
preg_match_all($re_question, $str, $matches_question, PREG_SET_ORDER, 0);
//if (count($matches_question) > 0) {
foreach($matches_question as $sentence_question){
    $answer_number = 0;
    $each_question = $sentence_question[1];
    $question_number +=1;
preg_match_all($re_answer, $str, $matches_answer, PREG_SET_ORDER, 0);
foreach($matches_answer as $sentence_answer){
    $questions_and_answers[] = $question_number. ' Q ' .$each_question. ' A ' .$answer_number.' - '.$sentence_answer[2];
    $answer_number +=1;
}    
//$result = array_merge($questions);
$result = array_merge([$questions_and_answers]);
 
}

print_r($result);
?>

你能帮我解决这个问题吗?

63lcw9qa

63lcw9qa1#

注意,答案与每个问题嵌套在一起--不仅foreach需要嵌套,main regex也需要嵌套。

<?php

//$str = ...

// $re_question_and_answers = '@\<b\>\d+\.\s*(?P<QUESTION>.*\?)\</b\>(?P<ANSWERS>(?:\<div[^\>]*\>\s*\<input[^\>]* value\=\"[^\"\)]+\)\s*[^\"]+\"\>(?:[^\<\>]*\</div\>)+' /* note: double " </div>" after Answer 32 */ .')+)(\<br\>|$)@iU'; // or '...@simU' // GOOD! but long and inflexible/immutable for "<div>" and "</div>"
$re_question_and_answers = '@\<b\>\d+\.\s*(?P<QUESTION>.*\?)\</b\>'
    .'(?P<ANSWERS>'
    .'(?:'  // (start of 1 ANSWER)
    .'([^\<\>]*|\</?(?!b\>)[^/\>]*\>)*' /* not "<tags>", "</tags>", or tags but not "<b>", "</b>" */
    .'\<input[^\>]* value\=\"[^\"\)]+\)\s*[^\"]+\"\s*\>'
    .'([^\<\>]*|\</?(?!b\>)[^/\>]*\>)*' /* not "<tags>", "</tags>", or tags but not "<b>", "</b>" */
    .')+'   // (end of 1 ANSWER)
    .')'    // end of 'ANSWERS'
    .'(\<br\>|$)@iU';
$re_answer = '#<input[^>]* value="([a-z]\.\)(?P<ANSWER>.*?)")>#s';

$questions = "";
$answers = [];
$questions_and_answers = [];
$answer_number = 0;
$question_number = 0;

preg_match_all($re_question_and_answers, $str, $matches_question_and_answers, PREG_SET_ORDER, 0);
//print_r($matches_question_and_answers);
$key_number = 0;
foreach($matches_question_and_answers as $sentence_question){
    $each_question = $sentence_question['QUESTION'];
    $question_number ++;
    $answer_number = 0;
    preg_match_all($re_answer, $sentence_question['ANSWERS'], $matches_answer, PREG_SET_ORDER, 0);
    //print_r($matches_answer);
    foreach($matches_answer as $sentence_answer){
        $questions_and_answers[$question_number][$key_number] = $question_number. " Q $question_number." .$each_question. ' A ' .$answer_number.' - '.$sentence_answer['ANSWER'];
        $answer_number ++;
        $key_number ++;
    }
}   

echo 'Result: ';
print_r($questions_and_answers);

?>

请看https://onlinephp.io/c/76493

相关问题