douliang1369 2015-04-28 23:47
浏览 40
已采纳

仅回显URL的一部分(字符串)

I have an input field where the user can type an URL. He could type the URL in many different ways, for example:

No matter how he types it, I want to echo it like this: example.com

Only when it is an sub domain, then it should look like this: subdomain.example.com

How can I do this?

  • 写回答

2条回答 默认 最新

  • duandu2980 2015-04-29 01:28
    关注

    I allow myself to answer my own question:

    $url = 'https://www.example.com/index.php';
    $str = preg_replace('#^https?://|www.#', '', rtrim($url,'/'));
    echo (strpos($str,'/') !== false) ? strstr($str, '/', true) : $str;
    

    You can define $url however you want. The result is always example.com

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

报告相同问题?

悬赏问题

  • ¥15 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错