简体   繁体   English

PHP使用DOM解析HTML

[英]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();" 该代码似乎在“ $ 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? simple_html_dom.php文件是否实际退出并加载了? You might want to write that include similar to: 您可能要编写include类似于:

<?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. 因此,服务器上的PHP解析器将让您知道是否存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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