将文件元素读入数组并用作html表单元素

时间:2013-04-18 12:26:20

标签: php arrays file text

大家好,我一直试图这样做。你能否告诉我如何解决这个问题。 我有一个包含问题的文本文件,以及每个元素之间使用空格键间隔的各自的多项选择答案。 我已经能够读取文本文件行并将其放入数组中。但是现在很难实现的是如何将每个元素放到一个html表单元素中。这些是我的代码:

文字档案:

编号问题(a)(b)(c)(d) 1螺旋模型最重要的特征是需求分析。风险管理。质量管理。配置管理。 2最差的耦合类型是数据耦合。控制耦合。邮票联轴器。内容耦合。 3故障基础测试技术之一是单元测试。 beta测试。压力测试。突变检测。 4故障模拟测试技术是突变测试压力测试黑盒测试白盒测试 5 RS也被称为白盒测试规范压力测试综合测试黑盒测试

读取文本文件的页面:

`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>`

print_r显示以下内容:

数组([0] =&gt;数字[1] =&gt;问题[2] =&gt;(a)[3] =&gt;(b)[4] =&gt;(c)[5] = &gt;(d)1 [6] =&gt;螺旋模型最重要的特征是需求分析。[7] =&gt;风险管理。[8] =&gt;质量管理。[9] =&gt;配置管理。 2 [10] =&gt;最差类型的耦合是[11] =&gt;数据耦合。[12] =&gt;控制耦合。[13] =&gt;标记耦合。[14] =&gt;内容耦合.3 [15] =&gt;故障基础测试技术之一是[16] =&gt;单元测试。[17] =&gt; beta测试。[18] =&gt;压力测试。[19] =&gt;突变测试。 4 [20] =&gt;故障模拟测试技术是[21] =&gt;突变测试[22] =&gt;压力测试[23] =&gt;黑盒测试[24] =&gt;白盒测试5 [25] ] =&gt; RS也被称为[26] =&gt;白盒测试[27] =&gt;压力测试[28] =&gt;综合测试[29] =&gt;黑盒测试)

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

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

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

  1. 问题将以冒号(:)结束。
  2. 答案将以点(。)结束,所有这些都是最后一个,因为我们将使用问题的编号作为分隔符。
  3. 问题或答案不包含任何数字[0-9],否则会混淆正则表达式。
  4. 这是我手动修改以使文本有效的结果文本:

    $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行文本

    您可以在此处查看输出:output


    备注 这只是为了练习,但下次尝试研究更多,并提出具体问题,或者人们会忽略/ downvote你的,请仔细阅读Stackoverflow的常见问题解答