开发者

Getting image links between anchor tag via php dom parser

I am trying to read all links with in a given url.

here is code I am using :

$dom = new DomDocument();
@$dom->loadHTMLFile($url);
$urls = $dom->getElementsByTagName('a');

foreach ($urls as $url) {
        echo $url->innertext ." => ".$url->getAttribute('href');

Script giving all links of given url.

But problem here is I am not able to get image links (image inside anchor tag)

First I tried with

$url->nodeValue

But it was giving anchor text having text values only.

I want to read both images and text links. I want output in below formmat.

Input :

<a href="link1.php">first开发者_StackOverflow link</a>
<a href="link2.php"> <img src="imageone.jpg"></a>

Current Output:

first link => link1.php
=>link2.php with warning (Undefined property: DOMElement::$innertext )

Required Output :

first link => link1.php
<img src="imageone.jpg">=>link2.php 


innerText doesn't exist in PHP; it's a non-standard, Javascript extension to the DOM.

I think what you want is effectively an innerHTML property. There isn't a native way of achieving this. You can use the saveXML or, from PHP 5.3.6, saveHTML methods to export the HTML of each of the child nodes:

function innerHTML($node) {
    $ret = '';
    foreach ($node->childNodes as $node) {
        $ret .= $node->ownerDocument->saveHTML($node);
    }
    return $ret;
}

Note that you'll need to use saveXML before PHP 5.3.6

You could then call it as so:

echo innerHTML($url) ." => ".$url->getAttribute('href');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜