0

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

theCure
  • 25
  • 5
  • Well, you can remove "new lines" before applying your regex... This can help: http://stackoverflow.com/a/10805198/1519058 – Enissay Sep 16 '13 at 22:36
  • Thanks Enissay that is a good suggestion. I wanted to perform the preg_match before parsing the string as it is an email and I didn't want to alter its structure. Because some emails might have attachments, I wasn't sure if this was a good idea... – theCure Sep 16 '13 at 22:47
  • just use `$string = preg_replace('/\s+/','',$string);` before preg_match() – Alexandr Perfilov Sep 16 '13 at 22:52

1 Answers1

0

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.

alxzaur
  • 81
  • 3