dqqvravff05370501 2014-02-13 01:20
浏览 40
已采纳

在php中修剪超过需要的URL

I am entering URL's into my database and i was getting all possible entries

I have the following code that takes the http:// or http or www .com .co.uk away

but the problem is this

when I enter a site like hat.com its taking the 'h' away this happens with t, p, w, and if its .co.uk it only removes the .uk

$new = rtrim($url, "/");

$reverse = strrev( $new );
$new = rtrim($reverse, ".www");
$new = rtrim($reverse, "//:ptth");
$new = rtrim($reverse, ".www//:ptth");
$new = rtrim($reverse, "//:sptth");
$new = rtrim($reverse, ".www//:sptth");
$url = strrev( $new );

Whats have I missed and what would I have to add?

  • 写回答

1条回答 默认 最新

  • douan3019 2014-02-13 01:26
    关注

    Using a regular expression will help here:

    preg_replace('~(^https?(://(www\.)?)?|\.com$|\.co\.uk$)~', '', $url);
    

    The regular expression used will match:

    • http, https, http://, https://, http://www., https://www. at the beginning of the string
    • .com, .co.uk at the end of the string.

    See this example:

    php> $url = 'https://www.example.com';
    'https://www.example.com'
    php> preg_replace('~(^https?(://(www\.)?)?|\.com$|\.co\.uk$)~', '', $url);
    'example'
    php> $url = 'http://hat.com';
    'http://hat.com'
    php> preg_replace('~(^https?(://(www\.)?)?|\.com$|\.co\.uk$)~', '', $url);
    'hat'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置