2016-04-06 27 views
0

我在PHP中使用preg_split函數將段落拆分爲幾個句子。在php中用句號分隔字符串排除「a.m.」

在我的情況:

$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.

回答

0

您應該可以使用(*SKIP)(*FAIL)來阻止am/pm匹配。您可以在這裏閱讀更多關於此方法的信息,http://www.rexegg.com/regex-best-trick.html

​​

正則表達式演示: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.也應該被允許使用imodifier

+0

它的工作原理。非常感謝你。對於正則表達式初學者來說,這種情況非常困難。 –