PHP REGEX:从 URL 获取域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3442333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP REGEX: Get domain from URL
提问by Adam
What I want
我想要的是
I want to get from a URL
the domain
part so from http://example.com/
-> example.com
我希望从获得URL
的domain
一部分,所以从http://example.com/
- >example.com
Examples:
例子:
+----------------------------------------------+-----------------------+
| input | output |
+----------------------------------------------+-----------------------+
| http://www.stackoverflow.com/questions/ask | www.stackoverflow.com |
| http://validator.w3.org/check | validator.w3.org |
| http://www.google.com/?q=hello | www.google.com |
| http://google.de/?q=hello | google.de |
+----------------------------------------------+-----------------------+
I found some related questions in stackoverflow
but none of them was exactly what I was looking for.
我发现了一些相关的问题,stackoverflow
但没有一个是我正在寻找的。
Thanks for any help!
谢谢你的帮助!
回答by cletus
There's no need to use a regex for this. PHP has an inbuilt function to do just this. Use parse_url()
:
没有必要为此使用正则表达式。PHP 有一个内置函数可以做到这一点。使用parse_url()
:
$domain = parse_url($url, PHP_URL_HOST);
回答by Marcin ?urek
I use:
我用:
$domain = parse_url('http://' . str_replace(array('https://', 'http://'), '', $url), PHP_URL_HOST);
Because parse_url
doesn't return host key when schema is missing in $url
.
因为parse_url
当$url
.
回答by turbod
$tmp = parse_url($url);
$url = $tmp['host']
回答by fnkr
This is like the regex from theraccoonbearbut with support for HTTPS domains.
这就像来自 theraccoonbear的正则表达式,但支持 HTTPS 域。
if (preg_match('/https?:\/\/([^\/]+)\//i', $target_string, $matches)) {
$domain = $matches[1];
}
回答by Josh K
Assumes that http://
prefixes everything.
假设http://
前缀为所有内容。
$tmp = explode("/", $url);
$domain = $tmp[2];
回答by T.Todua
Best way i think:
我认为的最佳方式:
preg_match('/(http(|s)):\/\/(.*?)\//si', 'http://www.example.com/page/?bla=123#!@#$%^&*()_+', $output);
// $output[0] ------------> https://www.example.com/
回答by Eugen Mihailescu
I think the following regexp might answers your question.
我认为以下正则表达式可能会回答您的问题。
This diagramexplains how it works, or rather why it works :-)
这张图解释了它是如何工作的,或者更确切地说它为什么工作:-)
$regexp = '/.*\/\/([^\/:]+).*/';
// www.stackoverflow.com
echo preg_replace($regexp, '', 'http://www.stackoverflow.com/questions/ask');
// google.de
echo preg_replace($regexp, '', 'http://google.de/?q=hello');
// it works for the other input tests too ;-)
回答by haydenmuhl
Here's my quick and dirty solution.
这是我快速而肮脏的解决方案。
http://([^/]+).*
http://([^/]+).*
I haven't tested it, but it should grab anything between the http://
and the first slash.
我还没有测试过它,但它应该抓住http://
第一个斜线和第一个斜线之间的任何东西。
回答by theraccoonbear
if (preg_match('/http:\/\/([^\/]+)\//i', $target_string, $matches)) {
$domain = $matches[1];
}