PHP:将相对img src网址翻译为绝对 [英] PHP: Translate relative img src urls to absolute

查看:109
本文介绍了PHP:将相对img src网址翻译为绝对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cURL将一个HTML页面提取到一个字符串中,并将其加载到一个DOMDocument中。在那里我可以得到所有的img标签和它们的源属性。我现在的问题是......我怎样才能使这些网址是绝对的?



URL列表可以包含各种变体,例如:




  • foobar.jpg

  • http ://example.com/foobar.jpg

  • /foobar.jpg

  • ../ foobar.jpg

  • 文件夹/ foobar.jpg
    $ b如果HTML是从任意URL获取的,那么将这些图片网址转换为绝对URL的安全方式是什么? ?有没有一种方法可以考虑基本标记呢? 解决方案

这是一个很好的PHP示例。 / p>

 函数rel2abs($ rel,$ base){
// something
}

更好的例子:


I have fetched an HTML page using cURL into a string and loaded it up in a DOMDocument. There I can get all the img tags and their source attributes. My problem now is... how can I make these URLs absolute?

The list of URLs can contain all kinds of variants, for example:

  • foobar.jpg
  • http://example.com/foobar.jpg
  • /foobar.jpg
  • ../foobar.jpg
  • folder/foobar.jpg

If the HTML is fetched from an arbitrary URL, what is a safe way of converting these image URLs into absolute ones? Is there a way you can take the base tag into consideration too?

解决方案

Here is great PHP example how to do this.

function rel2abs($rel, $base) { 
// something
}

More good examples:

这篇关于PHP:将相对img src网址翻译为绝对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆