使用php&;更改属性;查询路径
我想使用PHP&QueryPath查找文档中的所有图像,然后修改其使用php&;更改属性;查询路径,php,querypath,Php,Querypath,我想使用PHP&QueryPath查找文档中的所有图像,然后修改其src,如下所示: 我想换衣服 http://test.com/test/name.jpg 到 我可以使用 $qp2 = $qp->find('body'); 现在,当我想在上面找到所有的img来更改src: foreach ($qp2->find('img') as $i) { //here change the src } 但当我执行 echo $qp2->html(); 我只看
src
,如下所示:
我想换衣服
http://test.com/test/name.jpg
到
我可以使用
$qp2 = $qp->find('body');
现在,当我想在上面找到所有的img
来更改src
:
foreach ($qp2->find('img') as $i) {
//here change the src
}
但当我执行
echo $qp2->html();
我只看到最后一张图片。问题出在哪里?像这样
foreach($qp2->find('img') as $key as $img) {
echo $img->html();
}
有时,在使用qp对象时,必须使用top()或end()。比如:
$qp=htmlqp($lpurl)
foreach($qp->find('img')作为$key=>$img){
打印($img->attr('src'));
$url=parse_url($img->attr('src'));
打印(url);
回声“
”;
如果(!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'),作为$key=>$script){
打印($script->attr('src'));
$url=parse_url($script->attr('src'));
打印(url);
如果(!isset($url['scheme'])&&&!isset($url['host'])&&!empty($url['path'])){
$newjs=$htmlpath.'/.$url['path'];
回声“
”;
echo“这是修改后的”。$newjs;
}
}
像这样吗
foreach($qp2->find('img') as $key as $img) {
echo $img->html();
}
有时,在使用qp对象时,必须使用top()或end()。比如:
$qp=htmlqp($lpurl)
foreach($qp->find('img')作为$key=>$img){
打印($img->attr('src'));
$url=parse_url($img->attr('src'));
打印(url);
回声“
”;
如果(!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'),作为$key=>$script){
打印($script->attr('src'));
$url=parse_url($script->attr('src'));
打印(url);
如果(!isset($url['scheme'])&&&!isset($url['host'])&&!empty($url['path'])){
$newjs=$htmlpath.'/.$url['path'];
回声“
”;
echo“这是修改后的”。$newjs;
}
}
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;
}
}