Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从一块纯文本中提取有序的问题和答案_Php_Regex_Strpos_Strlen - Fatal编程技术网

Php 如何从一块纯文本中提取有序的问题和答案

Php 如何从一块纯文本中提取有序的问题和答案,php,regex,strpos,strlen,Php,Regex,Strpos,Strlen,问题也可用于纯文本。我想能够使用PHP。但是,由于我的正则表达式信息不够,我尝试使用PHP函数来实现这一点 示例纯文本:(从第12个问题开始->第15个问题,共4个问题) 12.一名47岁男子投诉上牙部分缺失。该患者的病史表明因外伤导致牙齿缺失 持续了三个月。11号和12号失踪。13、21和22是 被2/3摧毁,并用?llings恢复。咬合是正颌的。 哪种假牙结构最适合这位患者, 考虑到他的职业是讲师?A.金属烤瓷 牙桥B.塑料牙桥C.固位卡环(bugel) 带附件的可摘局部义齿D.可摘局部层流

问题也可用于纯文本。我想能够使用PHP。但是,由于我的正则表达式信息不够,我尝试使用PHP函数来实现这一点

示例纯文本:(从第12个问题开始->第15个问题,共4个问题)

12.一名47岁男子投诉上牙部分缺失。该患者的病史表明因外伤导致牙齿缺失 持续了三个月。11号和12号失踪。13、21和22是 被2/3摧毁,并用?llings恢复。咬合是正颌的。 哪种假牙结构最适合这位患者, 考虑到他的职业是讲师?A.金属烤瓷 牙桥B.塑料牙桥C.固位卡环(bugel) 带附件的可摘局部义齿D.可摘局部层流义齿 上颌义齿,E.锻造焊接金属牙桥 多面中间部分13.一名7岁男孩被诊断患有epi- 中度腮腺炎(腮腺炎)。说出这件事最复杂的地方 疾病:A.睾丸炎B.结肠炎C.皮炎D.肺炎 胆囊炎14.在预防性检查期间,一名40岁男子 出现以下变化:边缘牙龈增大, 环形,发绀,与牙齿接触时轻微出血 探查没有痛苦。用卢格尔碘染色牙龈 溶液导致粘膜呈浅棕色。使 诊断:A.慢性卡他性龈炎B.急性卡他性龈炎 C.慢性卡他性牙龈炎恶化D.慢性肥厚性牙龈炎 牙龈炎E.泛发性牙周炎15.一名4岁男孩 诊断为急性上颌化脓性骨膜炎 起源于64颗牙齿。选择最佳治疗策略: A.64颗牙齿的拔除、截骨术、药物治疗B.64颗牙齿 拔牙、抗感染药物治疗C.牙髓学 64颗牙齿的治疗,抗感染药物治疗 D.64颗牙齿的牙髓治疗,骨膜切开术 E.截骨术、抗感染药物治疗

  • 每个问题的开头都有一个问题编号
  • 问题通常有5或4个选项
我做了什么?

 function gettingQuestionAndOptions($string, $start, $end)
    {
        $string = " " . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return "";
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;

        return substr($string, $ini, $len);
    }
if (gettingQuestionAndOptions($text, '?', "A. ")) {

    $parsedQuestion = gettingQuestionAndOptions($text, '', "?");
    $parsedA = gettingQuestionAndOptions($text, 'A. ', "B. ");
    $parsedB = gettingQuestionAndOptions($text, 'B. ', "C. ");
    $parsedC = gettingQuestionAndOptions($text, 'C. ', "D. ");
    $endQuestion = "?";

} else if (gettingQuestionAndOptions($text, ':', "A. ")) {

    $parsedQuestion = gettingQuestionAndOptions($text, '', ":");
    $parsedA = gettingQuestionAndOptions($text, 'A. ', "B. ");
    $parsedB = gettingQuestionAndOptions($text, 'B. ', "C. ");
    $parsedC = gettingQuestionAndOptions($text, 'C. ', "D. ");
    $endQuestion = ":";

} else {
}
如何使用它?

 function gettingQuestionAndOptions($string, $start, $end)
    {
        $string = " " . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return "";
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;

        return substr($string, $ini, $len);
    }
if (gettingQuestionAndOptions($text, '?', "A. ")) {

    $parsedQuestion = gettingQuestionAndOptions($text, '', "?");
    $parsedA = gettingQuestionAndOptions($text, 'A. ', "B. ");
    $parsedB = gettingQuestionAndOptions($text, 'B. ', "C. ");
    $parsedC = gettingQuestionAndOptions($text, 'C. ', "D. ");
    $endQuestion = "?";

} else if (gettingQuestionAndOptions($text, ':', "A. ")) {

    $parsedQuestion = gettingQuestionAndOptions($text, '', ":");
    $parsedA = gettingQuestionAndOptions($text, 'A. ', "B. ");
    $parsedB = gettingQuestionAndOptions($text, 'B. ', "C. ");
    $parsedC = gettingQuestionAndOptions($text, 'C. ', "D. ");
    $endQuestion = ":";

} else {
}
结果:

我试了4个小时,但结果还是不正常。如果问题中有数字或
A
B
,我会得到更糟糕的结果


  • 我怎样才能做得更好?还是我的错在哪里
  • 我可以用正则表达式获得更多正确的数据吗

  • 假设你的文章总是以一个数字开头,将它们分组应该是一件简单的事情,只需跟踪你当前的问题,然后搜索下一个问题。这将有助于减少误报

    <?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;
    }
    
    输出:

    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
                    )
    
            )
    
    )
    
    我将这段代码保持在非常未优化的状态,因此很容易理解,但简单的字符串操作是一种非常低成本的计算,因此它应该适合日常使用


    作为旁注,我注意到您正在丢失诸如“fl”(f/l连字)和“fi”(f/I连字)之类的字符。如果您从数据库或网页中提取这些信息,请确保您正确处理UTF-8字符。

    如果您一直在处理全文,您可能会通过知道始终从“1”开始来改进结果然后下一场比赛将是“2”。等等。是的,大部分第一个问题开始了。但是我用php得到的结果是不均衡的。例如,它位于问题的中间或选项不出现@miken32与regex或。更好的是,获取更正确的数据取决于输入的外观。如何删除问题内容和选项(从数组到字符串)@BobbleBobble是否可以将其打印为常规字符串?:)我试过了<代码>$makeJson=json\u encode($questions\u answers,json\u FORCE\u OBJECT);$toString=json_decode($makeJson);foreach($toString as$ts){echo$ts->answers[1];}但我没有成功。如何在代码中以纯文本打印问题和选项@Miken32这是一个简单的字符串数组,你可以用它做任何你喜欢的事情
    foreach($q问题){echo$q[“问题”];}
    是的,但我想我告诉你错了。我要求把问题分开打印出来。我需要这个来保存到数据库。因此,;是否可以单独打印问题和问题选项?-要写入数据库@miken32的每个
    foreach
    操作难道不可能吗?我想将问题和同一问题的选项保存到数据库中。那我怎么才能得到它呢你能把这个问题和这个问题作为一个字符串吗?谢谢你@miken32