Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要使用图片替换链接的帮助(php)_Php_Regex_Dom_Find_Hyperlink - Fatal编程技术网

需要使用图片替换链接的帮助(php)

需要使用图片替换链接的帮助(php),php,regex,dom,find,hyperlink,Php,Regex,Dom,Find,Hyperlink,可能重复: 我在href标签中有一些随机文本和图像,如下所示: <a title="Some title" rel="lightbox" href="http://www.test.com/DSCF0733.jpg"><img class="alignleft size-thumbnail wp-image-504" title="some title" src="http://www.test.com/Dhghjkhjl33-150x150.jpg" alt="descr

可能重复:

我在href标签中有一些随机文本和图像,如下所示:

<a title="Some title" rel="lightbox" href="http://www.test.com/DSCF0733.jpg"><img class="alignleft size-thumbnail wp-image-504" title="some title" src="http://www.test.com/Dhghjkhjl33-150x150.jpg" alt="description" width="145" height="145" /></a>

我想把它们都找出来,放到一个数组里。文本可以包含其他链接,但我们只需要rel lightbox。 求求你,救命

为简单起见,请使用:


但您也可以修改包含的文本或其他属性。(问题的预替换部分可能需要详细说明。)

您可以使用内置的、简单但有效且安全的正则表达式

<?php 
$site=file_get_contents('http://example.com');

$xml = new DOMDocument();
@$xml->loadHTML($site);


foreach($xml->getElementsByTagName('a') as $links) {
    //Check for lightbox within the link
    if($links->getAttribute('rel')=='lightbox'){ 
        //Assign
        $imgLinks[]=$links->getAttribute('href');
    }
}

print_r($imgLinks);
?>
loadHTML($site);
foreach($xml->getElementsByTagName('a')作为$links){
//检查链接中的lightbox
if($links->getAttribute('rel')==“lightbox'){
//分配
$imgLinks[]=$links->getAttribute('href');
}
}
打印(imgLinks);
?>

我认为OP希望找到
src
img标签的所有
attributes,这些标签位于
a
标签中,这些标签具有
rel=“lightbox”
。您正在(尝试)查找具有
rel=“lightbox”
a
标记的所有
href
值。还请注意,您拼写的是
'herf'
,而不是
'href'
。呵呵,打字错误;s op特别说了
href标签
,然后
找到所有标签并放入数组
谢谢
<?php 
$site=file_get_contents('http://example.com');

$xml = new DOMDocument();
@$xml->loadHTML($site);


foreach($xml->getElementsByTagName('a') as $links) {
    //Check for lightbox within the link
    if($links->getAttribute('rel')=='lightbox'){ 
        //Assign
        $imgLinks[]=$links->getAttribute('href');
    }
}

print_r($imgLinks);
?>