嗨,伙计们,我已经尝试了一段时间了。你能给我一个关于如何做到这一点的见解吗?我有一个文本文件,其中包含问题和它们各自的多项选择答案,每个元素之间用空格键分隔。我已经能够读取文本文件行并将其放入数组中。但是现在证明难以实现的是如何将每个元素都放到一个html表单元素中。以下是我的代码:
文本文件:
数字问题(a) (b) (c) (d) 1螺旋模型最重要的特点是需求分析。风险管理。质量管理。配置管理。2最糟糕的耦合类型是数据耦合。控制耦合。图章耦合。内容耦合。3基于故障的测试技术之一是单元测试。测试版测试。压力测试。突变测试。4故障模拟测试技术是突变测试、压力测试、黑盒测试、白盒测试、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>`
THe print_r将显示以下内容:
Array ( => Number 1 => => (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 => A故障模拟测试技术为21 =>突变测试22 =>压力测试23 =>黑盒测试24 =>白盒测试5 25 => RS也称为26 =>白盒测试27 =>压力测试28 =>集成测试29 =>黑盒测试)
发布于 2013-04-18 21:23:19
explode()
使用空格作为分隔符,这就是将每个单词作为数组元素获取的原因。explode()
只使用您给它的字符,并在遇到该字符时拆分字符串。
您的数据(文件)没有模式。所以你需要在文本中建立一些规则,否则你将无法分离你想要的信息:
我修改了你的文本,建立了一些规则:
这是我手动修改的结果文本,以使文本正常工作:
$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
Stackoverflow笔记这样做只是为了练习,但下次尝试进行更多的研究,并提出特定的问题,否则人们会忽略/否决您的,请仔细阅读有关Stackoverflow的常见问题
发布于 2013-04-18 20:49:59
您应该将数组格式化为多维数组,其中索引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>
https://stackoverflow.com/questions/16082935
复制相似问题