0

Hi guys I have been trying to do this for sometime. Would you please give me an insight on how to go about this. I have a text file containing questions and their respective multiple choice answer spaced using space bar between each element. I have been able to read and put the text file lines into arrays. But now what has prooved difficult to achieve is how to put each and every element to an html form element. these are my codes:

Text file:

Number Question (a) (b) (c) (d) 1 The most important feature of spiral model is requirement analysis. risk management. quality management. configuration management. 2 The worst type of coupling is Data coupling. control coupling. stamp coupling. content coupling. 3 One of the fault base testing techniques is unit testing. beta testing. Stress testing. mutation testing. 4 A fault simulation testing technique is Mutation testing Stress testing Black box testing White box testing 5 RS is also known as specification of White box testing Stress testing Integrated testing Black box testing

The page that reads the text file:

`html>
<head>
<title>read</title>
</head>
<body>
<b><u> QUESTIONS AND ANSWERS QUIZ</u></b <br /> 
<p>
<?php
$openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text       file");
$fileContents = fread($openFile, filesize("questionandanswers.txt"));
fclose($openFile);
$delimiter = "  ";
$myArray = explode($delimiter, $fileContents);
print_r($myArray);
?>
</p>
</body>
</html>`

THe print_r displays the following:

Array ( [0] => Number [1] => Question [2] => (a) [3] => (b) [4] => (c) [5] => (d) 1 [6] => The most important feature of spiral model is requirement analysis. [7] => risk management. [8] => quality management. [9] => configuration management. 2 [10] => The worst type of coupling is [11] => Data coupling. [12] => control coupling. [13] => stamp coupling. [14] => content coupling. 3 [15] => One of the fault base testing techniques is [16] => unit testing. [17] => beta testing. [18] => Stress testing. [19] => mutation testing. 4 [20] => A fault simulation testing technique is [21] => Mutation testing [22] => Stress testing [23] => Black box testing [24] => White box testing 5 [25] => RS is also known as specification of [26] => White box testing [27] => Stress testing [28] => Integrated testing [29] => Black box testing )

4

2 回答 2

0

explode()使用空格作为分隔符,这就是您将每个单词作为数组元素的原因。explode()只需使用您给它的字符并在遇到该字符时拆分字符串。

您的数据(文件)没有模式。所以你需要在文本中建立一些规则,否则你将无法分离你想要的信息:

我修改了你的文字,建立了一些规则:

  1. 问题将以冒号(:) 结束。
  2. 答案将以点 (.) 结尾,除了最后一个之外,所有答案都将使用问题的编号作为分隔符。
  3. 问题或答案不包含任何数字 [0-9],否则会混淆正则表达式。

这是我手动修改以使文本正常工作的结果文本:

$string = 'Number Question (a) (b) (c) (d) 1 The most important feature of spiral model is: requirement analysis. risk management. quality management. configuration management 2 The worst type of coupling is: Data coupling. control coupling. stamp coupling. content coupling 3 One of the fault base testing techniques is: unit testing. beta testing. Stress testing. mutation testing 4 A fault simulation testing technique is: Mutation testing. Stress testing. Black box testing. White box testing 5 RS is also known as: specification of White box testing. Stress testing. Integrated testing. Black box testing';

解决方案:之后我们可以使用一些代码来分离信息:

<html>
    <head>
    <title>read</title>
    </head>
    <body>
        <b><u> QUESTIONS AND ANSWERS QUIZ</u></b> <br />
<?php
$openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text       file");
$string = fread($openFile, filesize("questionandanswers.txt"));

//Regex to get from the first number to the string's end, so we ignore the Number Question... bit;
preg_match('/\d.*/', $string, $match);

//We get all the strings starting with a number until it finds another number (which will be the beginning of another question;
preg_match_all('/\d\D*/', $match[0], $results);

$qas = array(); // We prepare an array with all the questions/answers
foreach($results[0] as $result){
    //Separating the answer from the string with all the answers.
    list($question, $all_answers) = explode(':', $result);

    //Separating the different answers
    $answers_array = explode('.', $all_answers);

    //Stuffing the question and the array with all the answers into the previously prepared array;
    $qas[] = array('question' => $question, 'answers' => $answers_array);
}

//Looping through the array and outputting all the info into a form;
foreach($qas as $k => $v){
    echo "<label>{$v['question']}</label><br/>";
    echo "<select>";

    //we loop through $v['answers'] because its an array within the array with all the answers.
    foreach($v['answers'] as $answer){
        echo "<option>$answer</option>";//the output
    }
    echo "</select>";
    echo "<br/><br/>";
}

?>
    </body>
</html>

看起来很复杂,因为所有的评论,它们实际上不到 20 行文字

你可以在这里看到输出:输出


备注 这样做只是为了练习,但下次尝试进行更多研究,并提出具体问题,否则人们会忽略/否决您的问题,请仔细阅读 Stackoverflow 的常见问题解答

于 2013-04-18T13:23:18.717 回答
0

您应该将数组格式化为多维数组,其中索引 0 是问题:

Array
(
    [0] = array
    (
        [0] = "This is the first question";
        [1] = "Answer A";
        [2] = "Answer B";
    )
    [1] = array
    (
        [0] = "This is the second question";
        [1] = "Answer A";
        [2] = "Answer B";
        [3] = "Answer C";
    )
)

您现在可以通过以下方式包含它:

<form>
    <?php
        foreach($filecontent as $question)
        {
            echo '<p>' .$question[0] .'</p>';
            for($i = 1; $i < count($question); $i++)
            {
                echo '<input value="' .$question[$i] .'" />';
            }
        }
    ?>
</form>
于 2013-04-18T12:49:58.673 回答