开发者

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.

  1. How to add a judge if failed to open stream: then echo 'this is not a valid url';?
  2. How to make a judge if there is no p tag in foreach, then echo '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";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜