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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?