duannao1920 2012-09-11 07:10
浏览 40
已采纳

parse_url函数php

I'm extracting tlds from my urls content with php's parse_url. than I have an array of top level domains which are compared with the extracted top level domain if they match or not.

  $url = parse_url($tag->getAttribute('href'));

  if (in_array($url['host'], $affi_urls) || $url['host'] == "www.example.com"){   

    $tag->setAttribute('href', '/redirect.php?url='.$href);       


   }

this works fine if the ur['host'] contains the top level domain. if the url['host'] is a relative path than is a big mess overthere.

/redirect.php?url=/example/test

how could I avoid this case?

  • 写回答

2条回答 默认 最新

  • duanhuanbo5225 2012-09-11 07:22
    关注

    You need to save the hostname of the page that you're processing. If $url['host'] is empty, use that hostname in its place.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?