doujiu8918 2011-03-24 19:58
浏览 46
已采纳

在php中清理网址

I am trying to make a user submit link box. I've been trying all day and can't seem to get it working.

The goal is to make all of these into example.com... (ie. remove all stuff before the top level domain)

Input is $url =

Their are 4 types of url:

www.example.com...
example.com...
http://www.example.com...
http://example.com...

Everything I make works on 1 or 2 types, but not all 4.

How one can do this?

  • 写回答

3条回答 默认 最新

  • dplsnw7329 2011-03-24 20:00
    关注

    You can use parse_url for that. For example:

    function parse($url) {
        $parts = parse_url($url);
        if ($parts === false) {
            return false;
        }
        return isset($parts['scheme']) 
                ? $parts['host'] 
                : substr($parts['path'], 0, strcspn($parts['path'], '/'));
    }
    

    This will leave the "www." part if it already exists, but it's trivial to cut that out with e.g. str_replace. If the url you give it is seriously malformed, it will return false.

    Update (an improved solution):

    I realized that the above would not work correctly if you try to trick it hard enough. So instead of whipping myself trying to compensate if it does not have a scheme, I realized that this would be better:

    function parse($url) {
        $parts = parse_url($url);
        if ($parts === false) {
            return false;
        }
        if (!isset($parts['scheme'])) {
            $parts = parse_url('http://'.$url);
        }
        if ($parts === false) {
            return false;
        }
    
        return $parts['host'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP