解析“site.com”来自 PHP 中传递的变量?
我在名为 target_passthrough 的全局变量中传递各种 URL,因此页面的 URL 可能如下所示: http://www.mysite.com/index.php?target_passthrough=example。 com
或者类似的东西。该变量的格式可以是多种格式,例如(减去引号):
- "www.example.com"
- ".example.com"
- "example.com"
- "http://www.example.com"
- ".example.com/subdir/"
- ".example.com/subdir/page.php"
- "example.com/subdir/ page.php"
请注意其中一些以句点作为第一个字符,例如 2,5 和 6。
现在,我想做的是从任何可能的 PHP 场景中提取“example.com”并将其存储到变量中以便稍后回显。我尝试了 parse_url 但它给了我“www”(当它存在时),这是我不想要的。在 url 只是“example.com”的情况下,它返回一个空值。
我真的不知道如何进行正则表达式匹配,或者这是否是我所需要的,所以任何指导将不胜感激——在 php 方面并不是那么先进。
I'm passing through a variety of URLs in a global variable called target_passthrough, so the URL of a page might look like:
http://www.mysite.com/index.php?target_passthrough=example.com
Or something like that. Formats for that variable may be a variety of things such as (minus quotes):
- "www.example.com"
- ".example.com"
- "example.com"
- "http://www.example.com"
- ".example.com/subdir/"
- ".example.com/subdir/page.php"
- "example.com/subdir/page.php"
Please note how some of those have periods as the first character such as 2,5, and 6.
Now, what I am trying to do is pull out just "example.com" from any of those possible scenarios with PHP and store it to a variable to echo out later. I tried parse_url but it gives me the "www" when that is present, which I do not want. In instances where the url is just "example.com" it returns a null value.
I don't really know how to do regex matching or if that is even what I need so any guidance would be appreciated--not really that advanced at php.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所指出的,您可以使用 parse_url 来执行大部分操作为您工作,然后只需去掉
www
或前导点(如果存在)。采用最后两个“单词”的替代策略并不总是有效,因为存在诸如
www.example.co.uk
之类的域。使用此策略将为您提供co.uk
而不是example.co.uk
。没有简单的规则来确定哪些部分是域或子域。As you pointed out, you can use parse_url to do much of the work for you and then simply strip off the
www
or leading dot if it is present.An alternative strategy of taking the last two "words" won't always work because there are domains like
www.example.co.uk
. Using this strategy would give youco.uk
instead ofexample.co.uk
. There is no simple rule for determining which parts are the domain or the sub-domain.parse_url() 输出 URL 不同部分的数组。您获得空值是因为您仅引用数组中的第一项。 parse_url()
parse_url() outputs an array the different parts of the URL. You are getting null values because you are only referencing the first item in the array. parse_url()