3v4l.org

run code in 500+ PHP versions simultaneously
<?php class LogParser { public static function getIdsByMessage($xml, $message) { $matches = array(); $xml_parsed = simplexml_load_string($xml); foreach($xml_parsed as $item) { if($item->message == $message) { $attributes = $item->attributes(); $matches[] = (string) $attributes['id']; } } return $matches; } } $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <log> <entry id="1"> <message>Application started</message> </entry> <entry id="2"> <message>Application ended</message> </entry> </log> XML; print_r(LogParser::getIdsByMessage($xml, 'Application ended'));

preferences:
126.72 ms | 2689 KiB | 5 Q