简体   繁体   English

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

[英]PHP Regex Subject with Possible New Lines in Pattern

I have the following code which is an extract from a an email message stored in a variable: 我有以下代码是从存储在变量中的电子邮件中摘录的:

$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); 

I can't figure out a way to match CORRESPONDANCE_ID or the number in the subpattern if there is a newline that splits the pattern in the string that is being searched. 如果存在换行符以将要搜索的字符串中的模式拆分,我无法找出匹配CORRESPONDANCE_ID或子模式中数字的方法。

I have tried using the x and s modifiers at the end of the pattern but to no avail. 我尝试在模式结尾使用x和s修饰符,但无济于事。

Can anyone please help me accomplish this? 谁能帮我做到这一点? Thank you!! 谢谢!!

You must use replacement for newlines characters such as 您必须使用换行符替换,例如

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

or add these characters to regex pattern: 或将以下字符添加到正则表达式模式中:

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

In both cases you $string variable does NOT been modified. 在这两种情况下,都不会修改$string变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM