我想使用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(); 我只看到了最后一张图片。问题出在哪里?
发布于 2013-05-23 05:27:43
是像这样吗?
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;
}
}https://stackoverflow.com/questions/10749969
复制相似问题