I'm trying to create a script that scans an xml file for the closing element and if it exists, echo it. I would like to have it search the file to see if </xml1> or </xml2> exists, if so it should result the xml. Is it possible to add "or" to preg_match? Is there a more efficient way to do this? My code I use is below.
Thanks a lot! Chris
<?php $filename = 'file.xml'; $blank_file = 'blank.xml'; if (file_exists($filename)) { $xml = file_get_contents ($filename); if (preg_match("#</xml1>#", $xml)) { echo $xml; } else { $blank = file_get_contents ($blank_file); echo $blank; } } else { $blank = file_get_contents ($blank_file); echo "$blank"; } ?>
Since it just a single character, you could use a character class:
if (preg_match("#</xml[12]>#", $xml)) {
In a situation where you need to do an "or", you can use the "|" operator within a sub-pattern:
if (preg_match("#</xml(1|2)>#", $xml)) {