在PHP排除" a.m。"中完全停止分割字符串。

时间:2016-04-06 15:38:10

标签: php regex preg-split

我在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.

,我该如何排除这种情况

1 个答案:

答案 0 :(得分:0)

您应该可以使用(*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