【问题标题】:Change attribute using php & querypath使用 php 和 querypath 更改属性
【发布时间】:2012-05-25 07:05:06
【问题描述】:

我想使用 PHP & QueryPath 来查找文档中的所有图像,然后像这样修改它的src

我想改变

http://test.com/test/name.jpg  

http://example.com/xxx/name.jpg  

我可以使用

找到具体的类名
$qp2 = $qp->find('body');  

现在当我想在上面找到所有img 来更改src:

foreach ($qp2->find('img') as $i) {
    //here change the src
}  

但是当我执行时

echo $qp2->html(); 

我只看到最后一张图片。问题出在哪里?

【问题讨论】:

    标签: php querypath


    【解决方案1】:

    像这样?

    foreach($qp2->find('img') as $key as $img) {
        echo $img->html();
    }  
    

    当您重新使用 qp 对象时,有时您必须使用 top() 或 end()。比如:

    $qp = htmlqp($lpurl);

    foreach ($qp->find('img') as $key => $img){ 
      print_r($img->attr('src'));
      $url = parse_url ($img->attr('src'));
      print_r($url);
      echo '<br/>';
      if (!isset($url['scheme']) && !isset($url['host']) && !empty($url['path'])){
        $newimg = $htmlpath . '/' . $url['path'];
        $img->end()->attr('src', $newimg);
        echo $img->html();
      }
    }
    
    foreach ($qp->top()->find('script') as $key => $script){ 
      print_r($script->attr('src'));
      $url = parse_url ($script->attr('src'));
      print_r($url);
      if (!isset($url['scheme']) && !isset($url['host']) && !empty($url['path'])){
        $newjs = $htmlpath . '/' . $url['path'];
        echo '<br/>';
        echo 'this is the modified ' . $newjs;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 1970-01-01
      • 2016-08-13
      • 2020-09-12
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多