简体   繁体   中英

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.

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!!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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