开发者

PHP DOMElement getAttribute Absolute Path URL?

EDIT: To achieve the URL I wanted, you would have to normalize/canonicalize it, as pointed out my Marc. To accomplish this, you can use a class such as the pear class suggested by Marc, or you could try php's realpath.. however, realpath would not work for me because of some htaccess stuff I'm doing.. but you could use some alternative's such as the one I found here; which seemed to work fairly well.

NOTE: The realpath alternative (truepath) written by Christian Sciberras is a modified version of Sven Arduwies' original script.

Here is some pseudo HTML:

<body><img src="../resources/image.jpg"></body>

Here is the PHP:

$doc = new DOMDocument();
$doc->loadHTMLFile("http://www.foobar.com/project/");
$imageList = $doc->getElementsByTagName("img");
$imageCnt = $imageList->length;
for ($idx=0; $idx<$imageCnt; $idx++) {
    echo $image开发者_C百科List->item($idx)->getAttribute("src");
}

The result will be:

../resources/image.jpg

But in reality, what I want is:

http://www.foobar.com/resources/image.jpg

The incoming image paths will not always be consistent; they may be absolute or relative paths, not always from the same domain, and obviously not always the same path.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜