【问题标题】:Split string by full stop in php exclude "a.m."在php中通过句号拆分字符串排除“a.m.”
【发布时间】:2016-04-06 15:38:10
【问题描述】:

我正在使用 PHP 中的preg_split 函数将一个段落拆分为多个句子。

就我而言:

$str = 'Applicants can check the final result of Admissions through the online enquiry system. The online enquiry system will be available from 10:00 a.m. on November 16 (Wednesday).';

$arr = preg_split('/\./', $str);

当有a.m.p.m. 时如何排除这种情况?

【问题讨论】:

    标签: php regex preg-split


    【解决方案1】:

    您应该能够使用(*SKIP)(*FAIL) 阻止am/pm 匹配。您可以在此处阅读有关该方法的更多信息,http://www.rexegg.com/regex-best-trick.html

    [ap]\.m\.(*SKIP)(*FAIL)|\.
    

    正则表达式演示:https://regex101.com/r/uD9xD7/1

    演示:https://eval.in/548705

    PHP 用法:

    $str = 'Applicants can check the final result of Admissions through the online enquiry system. The online enquiry system will be available from 10:00 a.m. on November 16 (Wednesday).';
    
    $arr = preg_split('/[ap]\.m\.(*SKIP)(*FAIL)|\./', $str);
    
    print_r($arr);
    

    输出:

    Array
    (
        [0] => Applicants can check the final result of Admissions through the online enquiry system
        [1] =>  The online enquiry system will be available from 10:00 a.m. on November 16 (Wednesday)
        [2] => 
    )
    

    如果还应允许A.M.,请使用i modifier

    【讨论】:

    • 它有效。非常感谢。这种情况对于 Regex 初学者来说是相当困难的。
    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多