So here is what I need to do.
If an user enters this: http://site.com
I need to remove http://
so the string will be site.com
, if an user enters http://www.site.com
I need to remove http://www.
or if the user enters www.site.com
I need to remove www.
or he can also enter site.com
it will be good as well.
I have a function here, but doesn't work how I want to, and I suck at regex.
preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $_POST['link'])
Use filter_var()
instead.
if (filter_var($_POST['link'], FILTER_VALIDATE_URL)) {
// valid URL
} else {
// not valid
}
还有parse_url函数。
I don't think I'd use regex for this, since you're only really checking for what is at the beginning of the string. So:
$link = $_POST['link'];
if (stripos($link, 'http://') === 0)
{
$link = substr($link, 7);
}
elseif (stripos($link, 'https://') === 0)
{
$link = substr($link, 8);
}
if (stripos($link, 'www.') === 0)
{
$link = substr($link, 4);
}
should take care of it.
我总是和str_replace一起去哈哈
str_replace('http://','',str_replace('www.','',$url))
I think what you're looking for is a multi-stage preg_replace()
:
$tmp = strtolower($_POST['link']) ;
$tmp = preg_replace('/^http(s)?/', '', $tmp);
$domain = preg_replace('/^www./', '', $tmp) ;
This simplifies the required regex quite a bit too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.