简体   繁体   English

检查给定域名是否存在于url组php中

[英]check if given domain name present in set of urls php

I have an url whose format may be : 我有一个网址,其格式可能是:
www.discover.com www.discover.com
http://discover.com http://discover.com
http://www.discover.com http://www.discover.com
http://www.abcd.discover.com http://www.abcd.discover.com
discover.com discover.com

And i have another url which may be any of below format: 我还有另一个网址,可能是以下任何格式:
www.discover.com/something/smoething www.discover.com/something/smoething
http://discover.com/something/smoething http://discover.com/something/smoething
http://www.discover.com/something/smoething http://www.discover.com/something/smoething
http://www.abcd.discover.com/something/smoething http://www.abcd.discover.com/something/smoething
discover.com/something/smoething discover.com/something/smoething

Now i want to compare this two urls to check whether domain name "discover.com" is present in the second url. 现在,我想比较这两个URL,以检查第二个URL中是否存在域名“ discover.com”。

Am using below code : 我正在使用以下代码:

$domain1 = str_ireplace('www.', '', parse_url($urlItem1, PHP_URL_HOST));
    $domain2= str_ireplace('www.', '', parse_url($urlItem2, PHP_URL_HOST));

    if(strstr($domain2, $domain1))
    {
        return $domain2;
    }

Solution : 解决方案:

function url_comparison($url1, $url2) {
$domain1 = parse_url($url1,PHP_URL_HOST);
$domain2 = parse_url($url2,PHP_URL_HOST);

$domain1 = isset($domain1) ? str_ireplace('www.', '',$domain1) : str_ireplace('www.', '',$url1); 
$domain2 = isset($domain2) ? str_ireplace('www.', '',$domain2) : str_ireplace('www.', '',$url2);

if(strstr($domain2, $domain1))
{
    return true;
}
else
{
    return false;
}
}

$url1 = "discover.com";
$url2 = "https://www.abcd.discover.com/credit-cards/resources/balance-transfer.shtml";

if(url_comparison($url1, $url2))
{
echo "Same Domain";
}
else
{
echo "Diffrent Domain";
}

Thanks. 谢谢。

Make use of the documentation, parse url 利用文档, 解析URL

Then you should look at the hostname, and with use of strpos. 然后,您应该查看主机名,并使用strpos。

$url = parse_url('www.discover.com/something/smoething');

if (strpos($url['host'], 'discover.com') !== false) {
  // do you thing
}

0 is also a valid value so the !== or === is needed 0也是有效值,因此需要!==或===

To check if two domain are equal you need to set some rules, because is www.example.com the same as example.com, and is https the same as http? 要检查两个域是否相等,您需要设置一些规则,因为www.example.com与example.com是相同的,并且https与http是相同的吗?

function url_comparison($url_1, $url_2, $www = false, $scheme = false) {
    $url_part_1 = parse_url($url_1);
    $url_part_2 = parse_url($url_2);

    if ($scheme && $url_part_1['scheme'] !== $url_part_2['scheme']) {
        return false;
    }

    if ($www && $url_part_1['host'] === $url_part_2['host']) {
        return false;
    } elseif(!$www && (strpos($url_part_1['host'], $url_part_2['host']) !== false || strpos($url_part_2['host'], $url_part_1['host']) !== false)) {
        return false;
    }

    return true;
}

With the above function you should see the right direction, not tested so should be tweaked perhaps. 使用上面的功能,您应该看到正确的方向,未经测试,因此也许应该进行调整。 The first 2 values should be an url. 前两个值应为网址。 $www is a boolean if the 'www.' $www是布尔值,如果“ www。” should be checked, and if $scheme = true also the https or http needs to be the same 应该检查,如果$scheme = true则https或http也必须相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM