在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?

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'