phpxmlregexepp

Using preg_match_all() to locate XML within a log in PHP


I have several log files that contain EPP requests and responses. I need to use preg_match_all to return an array of all the XML requests within the log, so I can then parse out the XML and validate it, however I'm not too familiar with regex. The XML should always begin with...

<?xml

...and end with...

</epp>

How would I form Regex to find everything contained between and including these identifiers.


Solution

  • You should use s flag in which . matches new lines as well, and a whole capture group:

    preg_match_all("/(<\?xml.*?<\/epp>)/s", $content, $matches);
    print_r($matches[1]);