嗨,伙计们,我已经尝试了一段时间了。请你给我一个关于如何做这件事的见解。我有一个文本文件,其中包含问题和各自的多选答案,每个元素之间用空格键隔开。我已经能够读取并将文本文件行放入数组中。但现在很难实现的是如何将每个元素都放在html表单元素中。这些是我的代码:
文本文件:
编号问题(a((b((c((d(1螺旋模型最重要的特点是需求分析。风险管理。质量管理。配置管理。2最糟糕的耦合类型是数据耦合。控制联轴器。冲压耦合。内容耦合。3故障基础测试技术之一是单元测试。β测试。压力测试。突变测试。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]=>编号[1]=>问题[2]=>(a([3]=>(b([4]=>(c([5]=>(d(1[6]=>螺旋模型最重要的特征是需求分析。[7] =>风险管理。[8] =>质量管理。[9] =>配置管理。2[10]=>最差的耦合类型是[11]=>数据耦合。[12] =>控制联轴器。[13] =>冲压联轴器。[14] =>内容耦合。3[15]=>故障基础测试技术之一是[16]=>单元测试。[17] =>测试版。[18] =>压力测试。[19] =>突变检测。4[20]=>一种故障模拟测试技术是[21]=>突变测试[22]=>压力测试[23]=>黑盒测试[24]=>白盒测试5[25]=>RS也称为[26]规范=>白盒检测[27]=>压力测试/28]=>集成测试[29]=>黑盒检测(
explode()
使用空格作为分隔符,这就是为什么将每个单词作为数组元素。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>
由于所有的评论,看起来很复杂,它们实际上不到20行的文本
你可以在这里看到输出:输出
备注这样做只是为了练习,但下次尝试更多的研究,并提出特定的问题,否则人们会忽略/否决你的,好好阅读Stackoverflow的常见问题
您应该将数组格式化为多维数组,其中索引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>