douzhi7070 2013-08-05 21:07
浏览 23
已采纳

使混合链接绝对

I'm outputting all the links on a page, and I want to make them hyperlinks.

The problem is I can't control the source document's URLs. Sometimes they're relative, sometimes they're absolute, and sometimes they're mixed.

Is there a simple way to make all relative links absolute, while still retaining links that were absolute in the first place?

I've looked at other questions and they seem to be focused on using regex or something similar to add the site domain to the beginning of the URL, which would work fine if all the URLs were relative.

If it matters, here's the code I'm using:

$html = file_get_contents(-[my url]-);
$doc = new DOMDocument(); 

libxml_use_internal_errors(true); //to hide error messages

$doc->loadHTML($html);
libxml_clear_errors();
$links = $doc->getElementsByTagName('a'); 

foreach($links as $link) 
{
    echo $link->getAttribute('href') . '<br />';
}
  • 写回答

1条回答 默认 最新

  • dongsuiwo0279 2013-08-05 21:28
    关注

    There three cases:

    1. absoulte urls - starting with [protocol]://
    2. relative to host urls - starting with /
    3. relative to host/path urls - all others

    First parse_url (that from file_get_contents), then simple if/elseis/else on each link based on three points above

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助