Regex doesn't work with multiple lines
$regpattern4 = "!<media:description type='plain'> (.*) <\/media:description>!";
I am parsing an XML document. The above Regex works if there are no line breaks in the description, but how do I make it work even if there are line breaks?
The Template Modifiers " man page may interest you, especially s ( PCRE_DOTALL
):
If this modifier is set, the dot metacharacter in the pattern matches all characters, including newlines . Without it, newlines are excluded. This modifier is equivalent to Perl / s. A negative class such as [^ a] always matches a newline character, regardless of the setting of this modifier.
Your regex will look something like this:
$regpattern4 = "!<media:description type='plain'> (.*) <\/media:description>!s";
Note. I added the modifier ' s
' after the trailing separator.
Why are you using regex to parse the xml? Why not use simplexml_load_string to load an XML document and "walk" through it. It will be less error prone than complex regex expressions unless you want to do a simple substitution.
Try
preg_match("/pattern/s", $text)
You need to add the s
(DOTALL) modifier :
$regpattern4 = "!(.*)<\/media:description>!s";