简体   繁体   中英

Parsing HTML with PHP code

I have some files that come in that also have php server code in them. The are expected. I'm wondering if there are is a way with SimpleXMLDom to parse the elements even though there are php tags in the document.

Example

<html>
    <? if(false) { ?>
    Never print this
    <? } ?>
</html>

I'd like to get the contents of html in plain text with the php server tags. It seems like just loading this block of code will cause exceptions.

I know it's really easy to eval the entire block by the following:

eval('?>'.$html.'<?');

but what I'd like to be able to do is pull an element and if there is php evaluate the code, much like was given below, but it doesn't seem I can load html code with php and parse it via SimpleXMLDom if there is a trick with the library or another that will do this simply

you can try with

<?php 
   $xml=simplexml_load_file("your_file.php");


   foreach($xml->children() as $child)
   {
     if (substr(ltrim($child->getName(), " \t"), 0,1) == "?">{
        you have php code
     }
   }
?>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM