PHP正则表达式主题与模式中可能的新行(PHP Regex Subject with Possible New Lines in Pattern)

编程入门 行业动态 更新时间:2024-10-25 20:21:13
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修饰符,但无济于事。

任何人都可以帮我完成这个吗? 谢谢!!

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.

I have tried using the x and s modifiers at the end of the pattern but to no avail.

Can anyone please help me accomplish this? Thank you!!

最满意答案

您必须使用替换换行符等字符

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变量都没有被修改。

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.

更多推荐

本文发布于:2023-07-21 09:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1208983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   主题   正则表达式   PHP   Pattern

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!