解析“site.com”来自 PHP 中传递的变量?

发布于 2024-09-17 21:21:22 字数 816 浏览 4 评论 0原文

我在名为 target_passthrough 的全局变量中传递各种 URL,因此页面的 URL 可能如下所示: http://www.mysite.com/index.php?target_passthrough=example。 com

或者类似的东西。该变量的格式可以是多种格式,例如(减去引号):

  1. "www.example.com"
  2. ".example.com"
  3. "example.com"
  4. "http://www.example.com"
  5. ".example.com/subdir/"
  6. ".example.com/subdir/page.php"
  7. "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):

  1. "www.example.com"
  2. ".example.com"
  3. "example.com"
  4. "http://www.example.com"
  5. ".example.com/subdir/"
  6. ".example.com/subdir/page.php"
  7. "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够钟 2024-09-24 21:21:22

正如您所指出的,您可以使用 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 you co.uk instead of example.co.uk. There is no simple rule for determining which parts are the domain or the sub-domain.

美人如玉 2024-09-24 21:21:22

parse_url() 输出 URL 不同部分的数组。您获得空值是因为您仅引用数组中的第一项。 parse_url()

 Array (
        [scheme] => http
        [host] => hostname
        [user] => username
        [pass] => password
        [path] => /path
        [query] => arg=value
        [fragment] => anchor 
         )

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()

 Array (
        [scheme] => http
        [host] => hostname
        [user] => username
        [pass] => password
        [path] => /path
        [query] => arg=value
        [fragment] => anchor 
         )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文