src
<img>
mutlak url almak için genel işlevinin nasıl uygulanacağını, http://test.com/directory/hi.html
gibi, hedef web sayfasının url yanı sıra, bilinen Görüntünün?
I would:
parse_url() burada seçim fonksiyonu olacak. Ben cesur hissediyordum, bu yüzden senin için uygulanan:
function getAbsoluteImageUrl($pageUrl,$imgSrc)
{
$imgInfo = parse_url($imgSrc);
if (! empty($imgInfo['host'])) {
//img src is already an absolute URL
return $imgSrc;
}
else {
$urlInfo = parse_url($pageUrl);
$base = $urlInfo['scheme'].'//'.$urlInfo['host'];
if (substr($imgSrc,0,1) == '/') {
//img src is relative from the root URL
return $base . $imgSrc;
}
else {
//img src is relative from the current directory
return
$base
. substr($urlInfo['path'],0,strrpos($urlInfo['path'],'/'))
. '/' . $imgSrc;
}
}
}
//tests
$host = 'http://test.com/directory/hi.html';
$imgSrc = '/images/lolcat.jpg';
echo getAbsoluteImageUrl($host,$imgSrc);
//echos http//test.com/images/lolcat.jpg
$host = 'http://test.com/directory/hi.html';
$imgSrc = 'images/lolcat.jpg';
echo getAbsoluteImageUrl($host,$imgSrc);
//echos http//test.com/directory/images/lolcat.jpg
$host = 'http://test.com/directory/hi.html';
$imgSrc = 'http://images.com/lolcat.jpg';
echo getAbsoluteImageUrl($host,$imgSrc);
//echos http://images.com/lolcat.jpg
parse_url, sen böyle bir şey yapabilirsiniz:
"$scheme://$domain$img_url"
gibi img url oluşturmak"$scheme://$domain/$path/$img_url"
gibi img url oluşturmak. Bu sunucu tarafından çözümlenen olacak çünkü img_url, ".. /. /" Tarzı ise, bu da çalışır.Ayrıca bir programa veya etki alanı ('xx :/ /' veya 'xx.x /') ile başlayan img adresler hariç isteyebilirsiniz.
Bu konu size yardımcı olabilir:
http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php