|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64946 DomDocument getAttribute return empty on IMG/src, LINK/href, with protocol-rela
Submitted: 2013-05-29 22:29 UTC Modified: 2013-05-29 22:55 UTC
From: work at danemacmillan dot com Assigned:
Status: Closed Package: *XML functions
PHP Version: 5.4.15 OS: centos6.4 (64bit), win7 (64bit)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: work at danemacmillan dot com
New email:
PHP Version: OS:


 [2013-05-29 22:29 UTC] work at danemacmillan dot com
DomDocument's getAttribute will return an empty string on both an IMG tag's SRC attribute, and a LINK's HREF attribute *when* the URLs provided are protocol-relative. 

In the description below I'm going to refer to IMG tags only, but the same issue stands for the LINK tag's HREF attribute as well. There may be others, but these are the only two I discovered. 

The problem arises when scraping the SRC of IMG tags. If the IMG tag SRC has a protocol-relative URL (an absolute path beginning with "//" instead of "http://"), it will be unreadable. Relative paths are readable (e.g., "/img/landing.png").

I've used both the getElementsByTagName method and the Xpath method. They both suffer from the same problem. However, the moment I prepend any absolute URL with its designated protocol, the IMG SRC is readable. 

This problem does not exist for the A tag HREF attribute, nor the SCRIPT tag SRC attribute; in each case the URL provided will be returned, regardless of the URL format.

Test script:
To summarize, the first two URLs will be readable, and the third URL will *not* be readable:

<img src="" />
<img src="/img/eg.png" />
<img src="//" />

I'll demonstrate two ways to grab the URLs, and they both fail with protocol-relative URLs.

// use for both examples:
$html = file_get_contents("");

// one way (Xpath)
$dom = new DOMDocument();
$x = new DOMXPath($dom); 
$data = array();
foreach($x->query('//img') as $node) {
    $data['img']['src'][] = urldecode($node->getAttribute('src'));

// another way (getElementsByTagName)
$doc = new DOMDocument();
$imgs = $doc->getElementsByTagName('img');
$data = array();
for ($i = 0; $i < $imgs->length; $i++) {
    $img = $imgs->item($i);
        $data[] = urldecode($img->getAttribute('src'));

// print results from either

Expected result:
Array ( [img] => Array ( [src] => Array ( [0] => // ) ) )

Actual result:
Array ( [img] => Array ( [src] => Array ( [0] =>  ) ) )


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-29 22:55 UTC] work at danemacmillan dot com
-Status: Open +Status: Closed
 [2013-05-29 22:55 UTC] work at danemacmillan dot com
I don't know how, but the exact same code did not work for two days. After reporting a bug, it works.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Sep 30 17:01:24 2023 UTC