简体   繁体   中英

PHP parsing html with DOM

I am trying to get the below code to work, so that I understand what I am doing on a larger project. For the most part,I'm just trying to make sure I can grab elements by their tag. The code seems to break at "$html = new simple_html_dom();" because if I comment that out, then I get the two print outs. but if it's not commented out, nothing shows up on the screen at all.

<?php
# create and load the HTML

include('simple_html_dom.php');
print "hello ";
$html = new simple_html_dom();

print "world"
#$html->load("<html><body><p>Hello World!</p><p>We're here</p></body></html>");

# get an element representing the second paragraph
#$element = $html->find("p");
# modify it
#$element[1]->innertext .= " and we're here to stay.";

# output it!
#echo $html->save();
?>

Is the simple_html_dom.php file actually exiting and loaded? You might want to write that include similar to:

<?php
if(!@file_exists('./simple_html_dom.php') ) 
{
    echo 'can not include: simple_html_dom.php';
    die();
} 
else 
{
    include('./simple_html_dom.php');
}
?>

So the PHP parser on the server will let you know if there is a problem.

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