假设您的文本始终以数字开头,将它们分组应该很简单,只需跟踪您当前的问题,然后搜索下一个问题。这将有助于减少误报。
<?php
$data = "12.A 47-year-old man complains of partial loss of his upper teeth. The patient’s medi- cal history states loss of teeth due to trauma sustained 3 months ago. 11 and 12 are lost. 13, 21, and 22 are destroyed by 2/3 and restored with?llings. Occlusion is orthognathic. What denture construction would be optimal for this patient, considering his occupation as a lecturer? A.Porcelain-fused-to-metal dental bridge B.Plastic dental bridge C.Clasp-retained (bugel) removable partial denture with attachments D.Removable partial laminar denture for the upper jaw E.Swaged-soldered metal dental bridge with faceted intermediate part 13.A 7-year-old boy is diagnosed with epi- demic parotitis (mumps). Name the most li- kely complication of this disease: A.Orchitis B.Colitis C.Dermatitis D.Pneumonia E.Cholecystitis14.During preventive examination a 40-year- old man presents with the following changes: marginal gingiva is enlarged, torus-shaped, cyanotic, slightly bleeding when touched wi- th a dental probe; there is no pain. Staining the gums with Lugol’s iodine solution results in light-brown coloring of mucosa. Make the diagnosis: A.Chronic catarrhal gingivitis B.Acute catarrhal gingivitis C.Exacerbation of chronic catarrhal gingivitis D.Chronic hypertrophic gingivitis E.Generalized periodontitis 15.A 4-year-old boy has been diagnosed wi- th acute purulent periostitis of the upper jaw originating from the 64 tooth. Choose the optimal treatment tactics: A.The 64 tooth extraction, periosteotomy, pharmacotherapy B.The 64 tooth extraction, anti-in?ammatory pharmacotherapy C.Endodontological treatment of the 64 tooth, anti-in?ammatory pharmacotherapy D.Endodontological treatment of the 64 tooth, periosteotomy E.Periosteotomy, anti-in?ammatory pharmacotherapy";
$questions = [];
$question_num = 12;
$previous_question_start = 0;
while (true) {
$question_start = strpos($data, "$question_num.", $previous_question_start);
$question_end = strpos($data, (++$question_num) . ".", $question_start);
if ($question_end === false) {
// no more matches, this is the last question
$questions[] = substr($data, $question_start);
break;
}
$questions[] = substr($data, $question_start, $question_end - $question_start);
$previous_question_start = $question_start;
}
此时,每个问题都包含在一个数组元素中,因此请使用相同的技术在每个问题中搜索您的答案。我们会将结果保存到一个新数组中。
$questions_answers = [];
$answer_choices = ["A", "B", "C", "D", "E"];
foreach ($questions as $q) {
$qa = [];
$qa["question"] = substr($q, 0, strpos($q, "$answer_choices[0]."));
$previous_answer_start = 0;
foreach ($answer_choices as $i=>$a) {
$answer_start = strpos($q, $answer_choices[$i]. ".", $previous_answer_start);
if (empty($answer_choices[$i + 1])) {
// end of the array, this is the last answer
$qa["answers"][] = substr($q, $answer_start);
break;
}
$answer_end = strpos($q, $answer_choices[$i + 1] . ".", $answer_start);
$qa["answers"][] = substr($q, $answer_start, $answer_end - $answer_start);
$previous_answer_start = $answer_start;
}
$questions_answers[] = $qa;
}
print_r($questions_answers);
输出:
Array
(
[0] => Array
(
[question] => 12.A 47-year-old man complains of partial loss of his upper teeth. The patient’s medi- cal history states loss of teeth due to trauma sustained 3 months ago. 11 and 12 are lost. 13, 21, and 22 are destroyed by 2/3 and restored with?llings. Occlusion is orthognathic. What denture construction would be optimal for this patient, considering his occupation as a lecturer?
[answers] => Array
(
[0] => A.Porcelain-fused-to-metal dental bridge
[1] => B.Plastic dental bridge
[2] => C.Clasp-retained (bugel) removable partial denture with attachments
[3] => D.Removable partial laminar denture for the upper jaw
[4] => E.Swaged-soldered metal dental bridge with faceted intermediate part
)
)
[1] => Array
(
[question] => 13.A 7-year-old boy is diagnosed with epi- demic parotitis (mumps). Name the most li- kely complication of this disease:
[answers] => Array
(
[0] => A.Orchitis
[1] => B.Colitis
[2] => C.Dermatitis
[3] => D.Pneumonia
[4] => E.Cholecystitis
)
)
[2] => Array
(
[question] => 14.During preventive examination a 40-year- old man presents with the following changes: marginal gingiva is enlarged, torus-shaped, cyanotic, slightly bleeding when touched wi- th a dental probe; there is no pain. Staining the gums with Lugol’s iodine solution results in light-brown coloring of mucosa. Make the diagnosis:
[answers] => Array
(
[0] => A.Chronic catarrhal gingivitis
[1] => B.Acute catarrhal gingivitis
[2] => C.Exacerbation of chronic catarrhal gingivitis
[3] => D.Chronic hypertrophic gingivitis
[4] => E.Generalized periodontitis
)
)
[3] => Array
(
[question] => 15.A 4-year-old boy has been diagnosed wi- th acute purulent periostitis of the upper jaw originating from the 64 tooth. Choose the optimal treatment tactics:
[answers] => Array
(
[0] => A.The 64 tooth extraction, periosteotomy, pharmacotherapy
[1] => B.The 64 tooth extraction, anti-in?ammatory pharmacotherapy
[2] => C.Endodontological treatment of the 64 tooth, anti-in?ammatory pharmacotherapy
[3] => D.Endodontological treatment of the 64 tooth, periosteotomy
[4] => E.Periosteotomy, anti-in?ammatory pharmacotherapy
)
)
)
我没有对这段代码进行优化,所以它很容易理解,但是简单的字符串操作是一种非常低成本的计算,所以它应该适合日常使用。
附带说明,我注意到您丢失了诸如“f”(f/l 连字)和“fi”(f/i 连字)之类的字符。如果您从数据库或网页中提取此信息,确保您正确处理 UTF-8 字符。