2 questions about `file_get_html`
I want to use simple_html_dom.php
to do some work.
$html = file_get_html('http://www.domain.com');
foreach($html->find('p') as $el开发者_运维知识库ement) {
echo $element;
}
I have 2 problems.
- How to add a judge if
failed to open stream
: thenecho 'this is not a valid url';
? - How to make a judge if there is no
p
tag inforeach
, thenecho 'Can not find p tag';
?
Thanks.
That's part of the problem with Simple_HTML_DOM
... file_get_html()
always return a valid object regardless if the load failed or not. Creating your own instance doesn't help either... There is no actual way of knowing if your file parsed properly or not.
As for finding out if you actually have <p>
elements in your result:
$pTags = $html->find('p');
if(empty($pTags)) {
echo 'Cannot find p tag';
} else {
foreach($pTags as $element) {
echo $element;
}
}
Overall, I would recommend dropping Simple_HTML_DOM
and migrate your code to phpQuery
instead (on a plus side, phpQuery
doesn't do its own parsing, it is simply a wrapper for PHP's DOMDocument class). The API is more streamlined and allows you to know if parsing succeeded or not.
try {
$html = phpQuery::newDocument($sourceCode);
$pTags = $html->find('p');
if(empty($pTags)) {
echo 'Cannot find p tag';
} else {
foreach($pTags as $element) {
$element = pq($element); // Wrap raw DOMNode in phpQuery object instance;
echo $element->html();
}
}
} catch(Exception $ex) {
echo $ex->getMessage();
}
try this:
$html = file_get_html('http://www.domain.com') or die('this is not a valid url');
$p = $html->find('p');
if(count($p) <=0){
die('Can not find p tag')
}
foreach($p as $element) {
echo $element;
}
$html = new Simple_html_dom();
$ipaddrss='write your url here';
$html = file_get_html($ipaddrss);
$anchor=$html->find('dd[class=count]');//you can find the tags with its attributes like
//shown here
if($anchor) {
echo $anchor;
} else {
echo "sorry! no tags found";
}
精彩评论