PHP 正则表达式主题,模式中可能有新行


PHP Regex Subject with Possible New Lines in Pattern

>我有以下代码,它是存储在变量中的电子邮件的摘录:

$string = '<td colspan=3D"2"><span style=3D"display: none;">CORRES
PONDANCE_ID:4</=
span> <span style=3D"display: none;">CONFIRMATION_NUMBER:9986337900</s=
pan></td><td colspan=3D"2"><span style=3D"display: none;">CORRESPO
NDANCE_ID:4</=
span> <span style=3D"display: none;">CONFIRMATION_NUMBER:9986337900</s=
pan></td>';
preg_match('/CORRESPONDANCE_ID:([0-9]+)/', $string, $id_matches);

print_r($id_matches); 

如果有一个换行符在正在搜索的字符串中拆分模式,我无法找到匹配子模式CORRESPONDANCE_ID或数字的方法。

我尝试在模式末尾使用 x 和 s 修饰符,但无济于事。

谁能帮我完成这个?谢谢!!

必须使用替换换行符,例如

preg_match_all('/CORRESPONDANCE_ID:([0-9]+)/', 
               preg_replace('/'r?'n/', "", $string), $id_matches);

或将这些字符添加到正则表达式模式:

preg_match_all('/[A-Z'r'n]+_ID:([0-9]+)/', $string, $id_matches);

在这两种情况下,您$string变量都不会被修改。