Q
分割测验查询
-2
A
回答
0
function parseQuestions($str) {
$str = preg_replace("/\r|\n|\t/", " ", $str); // Remove line breaks and replace with spaces
$split_questions = preg_split("/\d+\./", $str, -1, PREG_SPLIT_NO_EMPTY); // An array containing each question and their answers as a string
$split_answers = array(); // Array that will contain the final arrays with questions and their answers
foreach($split_questions as $qkey => $q) { // Parses the question and splits it so [0] is the question and everything following are answers
$split_answers[$qkey] = preg_split("/[a-zA-Z]\)/", $q);
foreach($split_answers[$qkey] as $akey => $a) { // Loop that goes through each of the answers and trims off whitespace
$split_answers[$qkey][$akey] = trim($a);
}
}
return $split_answers;
}
此函数将返回一个包含您的问题的数组。数组的索引0将成为你的问题,所有后续索引将成为答案。例如,当提供时字符串:
1. What is the first letter of the alphabet?
A) no
B) 17
C) A
2. What time is it?
A) 7:02
B) Night Time
C) I don't know
D) Hammer Time!
该函数将返回一个数组具有以下结构:
[
[
"What is the first letter of the alphabet?",
"no",
"17",
"A"
],
[
"What time is it?",
"7:02",
"Night Time",
"I don't know",
"Hammer Time!"
]
]
空白移除并忽略,因此不应该影响到返回的数组。
+0
谢谢。这正是我所寻找的,但还有另一个问题。如果我在答案中有一些“(”),他会分开答案,如何解决它? –
+0
你得到的越具体,它就越不友好,触发这个的是你有一个字母后跟''' 。如果你的问题只有大写字母来表示每个选项(即'A)','B)'和'C)'而不是'd)'),那么改变'/ [a-zA-Z] \)/ '到'/ [AZ] \)/'。如果您的问题最多只有4个答案,那么答案是A,B,C和D,那么您可以将'/ [A-Z] \)/'更改为'/ [A-D] \)/'。如果你有一个例子,你需要被允许可以帮助我。如果这需要允许任何答案组合,那么您需要由谁提出问题来设置一些指导方针 – bugfroggy
0
PHP的代码可能是这样的:
$array = [
'q1' => [
'Ans1',
'Ans2'
],
'q2' => [
'Ans1',
'Ans2'
]
];
相关问题
- 1. SQL查询 - 分割行
- 2. MySQL数据库为考试(测验测验)查询积分
- 3. 如何写分割两行的查询?
- 4. 分割字符串中的MySQL查询
- 5. 子查询而使用分割功能
- 6. 用SQL查询分割订单
- 7. 检测分割标签
- 8. 加权分割测试
- 9. 物体检测+分割
- 10. ASP.NET A/B分割测试
- 11. SQL测验选择/子查询
- 12. 查询分析器 - eDisMax - 分割和短语
- 13. 命令查询分隔验证重试
- 14. 查找,分割和级联
- 15. 如何将数据集分割/分割为训练和测试数据集以进行交叉验证?
- 16. 图像分割中的T检验
- 17. MySQL的SELECT查询字符串分割搜索值
- 18. 如何使用Sybase查询功能来分割字符串
- 19. 在SQL Server中分割名称字段的查询
- 20. 错误SQL查询,同时分割字符串
- 21. 如何让MySQL查询文本被分割,
- 22. SQL查询在给定的时间间隔内分割数字
- 23. 将包含分割和toint的密码查询转换为C#
- 24. 分割字符串列正确地在我的查询空间
- 25. 分割Sqlite数据库python查询结果
- 26. 如何使用mysql查询分割这个字符串?
- 27. MySQL查询到CSV和一个字段分割
- 28. PHP:跨多行分割长查询字符串
- 29. 如何多分割字符串(WHERE IN查询)
- 30. 使用列分割字符串连接表的SQL查询
请编辑你的问题 –
刚刚做到了。你能帮我吗? –
你想拆分什么?给定格式的字符串? – invisal