在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?
我正在输入URL到我的数据库中,我得到了所有可能的条目 p>
我有以下代码,它采用 但问题是这个 p>
当我进入像hat.com这样的网站时 't,p,w会发生这种情况,如果它的.co.uk它只删除.uk p>
我错过了什么,我需要添加什么? p>
div> http:// code>或
http code>或
www code>
.com code>
.co.uk code> away p>
$ 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);
code> pre>
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'