开发者

Remove http from variable

I have a variable, such as this:

$domain = "http://test.com"

I need to use preg_replace or str_place to get the variable like this:

$domain = "test.com"

I have tried using the following, but they do not work.

1) $domain = preg_replace('; ((ftp|https?)://|www3?\.).+? ;', ' ', $domain);
2) $domain = pr开发者_如何学编程eg_replace(';\b((ftp|https?)://|www3?\.).+?\b;', ' ', $domain);

Any suggestions?


Or you can use parse_url:

parse_url($domain, PHP_URL_HOST);


$domain = ltrim($domain, "http://");


Did you try the str_replace?

$domain = "http://test.com"
$domain = str_replace('http://','',$domain);

You regular expressions probably don't find a match for the pattern.


preg_replace('~(https://|http://|ftp://)~',, '', $domain);


preg_match('/^[a-z]+:[/][/](.+)$/', $domain, $matches);
echo($matches[1]);

Should be what you are looking for, should give you everything after the protocol... http://domain.com/test becomes "domain.com/test". However, it doesn't care about the protocol, if you only want to support specific protocols such as HTTP and FTP, then use this instead:

preg_match('/^(http|ftp):[/][/](.+)$/', $domain, $matches);

If you only want the domain though, or similar parts of the URI, I'd recommend PHP's parse_url() instead. It does all the hard work for you and does it the proper way. Depending on your needs, I would probably recommend you use it anyway and just put it all back together instead.


simple regex:

preg_replace('~^(?:f|ht)tps?://~i','', 'https://www.site.com.br');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜