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

Php 将文件元素读入数组并用作html表单元素,php,arrays,file,text,Php,Arrays,File,Text,嗨,伙计们,我试着这么做已经有一段时间了。你能告诉我怎么做吗。 我有一个文本文件,包含问题和它们各自的选择题答案,每个元素之间用空格键隔开。 我已经能够读取并将文本文件行放入数组。但是现在很难实现的是如何将每个元素放到html表单元素中。这是我的密码: 文本文件: 数字问题(a)(b)(c)(d) 1螺旋模型最重要的特征是需求分析。风险管理。质量管理。配置管理。 2最糟糕的耦合类型是数据耦合。控制耦合。邮票耦合。内容耦合。 3基于故障的测试技术之一是单元测试。测试版。压力测试。突变测试。 4故障

嗨,伙计们,我试着这么做已经有一段时间了。你能告诉我怎么做吗。 我有一个文本文件,包含问题和它们各自的选择题答案,每个元素之间用空格键隔开。 我已经能够读取并将文本文件行放入数组。但是现在很难实现的是如何将每个元素放到html表单元素中。这是我的密码:

文本文件:

数字问题(a)(b)(c)(d) 1螺旋模型最重要的特征是需求分析。风险管理。质量管理。配置管理。 2最糟糕的耦合类型是数据耦合。控制耦合。邮票耦合。内容耦合。 3基于故障的测试技术之一是单元测试。测试版。压力测试。突变测试。 4故障模拟测试技术是突变测试、应力测试、黑盒测试和白盒测试 5RS也称为白盒测试规范压力测试集成测试黑盒测试

读取文本文件的页面:

`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>`
`html>
阅读

问答测验您应该将数组格式化为多维数组,其中索引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>

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

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

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

  • 问题将以冒号(:)结束
  • 答案将以一个点(.)结束,除最后一个外,所有答案都将以问题的编号作为分隔符
  • 问题或答案不会包含任何数字[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>
    
    
    阅读
    问答测验

    当我打印时,你能发布你在print\r($myArray)中得到的吗。我已经把它作为问题的一部分添加了下来。我还不太熟悉多维数组的使用,请你告诉我如何使用我的代码。提前感谢您的帮助。特别是从$myArray=explode($delimiter,$fileContents)开始;打印($myArray);?>

    “谢谢你,朋友。我想知道你是怎么做到的。在您的编码中,它与我之前的代码在哪里结合?我知道作为一个业余爱好者,我们有时会成为一个麻烦,但是我如何使用上述代码作为表单提交给php文件处理器来处理相同的代码呢。非常感谢你的朋友。我已经编辑了代码给你一个如何加入它的线索。使用$string而不是$fileContent。关于表单,这是非常基本的,但我不会在这里写一个关于表单的完整教程,谷歌一点并学习如何做,当你在StackOverflow遇到具体问题时,只要将上述代码以一种格式提交到Php文件,例如提交后,我可以使用$u POST方法验证发布的值。再次感谢。我该如何把它做成表格呢。