php regex 域字符串是否有 www 和顶级域

发布于 2024-10-09 22:36:42 字数 222 浏览 0 评论 0原文

当谈到正则表达式时,我有点业余,但考虑到我有以下三个域

www.google.com
www.google.co.uk
google.com

,我想创建一些正则表达式来测试该域是否有 www.co.uk.com

有人知道一些可以对此进行测试的正则表达式吗?

I'm abit of an amateur when it comes to regex but consider i have the following three domains

www.google.com
www.google.co.uk
google.com

I would like to create some regex that will test to see if the domain has a www and also a .co.uk or .com

Does anybody know of some regex that will test for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

天冷不及心凉 2024-10-16 22:36:42

你不需要正则表达式,你可以使用 strpos 据说比正则表达式更快。

if ( strpos('www.', $mystring) !== false ) {
    //www was found in the string
} else {
    //www was not foun in the string
}

如果您确实想要慢一些并使用正则表达式,您可以像这样测试所有这些

preg_match('/www|\.com|\.co\.uk/', $mystring);

如果例如您想对 www 应用与 .com 不同的逻辑,您可以使用

preg_match('/www/', $string);
preg_match('/\.com/', $string);
preg_match('/\.co\.uk/', $string);

You don't need regex for this , you can use strpos witch is sayd to be faster than regex .

if ( strpos('www.', $mystring) !== false ) {
    //www was found in the string
} else {
    //www was not foun in the string
}

If you realy whant to be slower and use regex you can test for all of them like this

preg_match('/www|\.com|\.co\.uk/', $mystring);

If for example you whant to apply different logic for www than for .com you can use

preg_match('/www/', $string);
preg_match('/\.com/', $string);
preg_match('/\.co\.uk/', $string);
标点 2024-10-16 22:36:42

试试这个:

/^www.*(?:com|co\.uk)$/

Try this:

/^www.*(?:com|co\.uk)$/
国际总奸 2024-10-16 22:36:42

根据我对您问题的理解,域名需要以 www 开头,并以 .co.uk.com 结尾。所以这是正则表达式:

<?php
    $domains = array(
        "www.google.com",
        "www.google.co.uk",
        "google.com"
    );
    foreach($domains as $domain){
        echo sprintf(
            "%20s -> %d\n",
            $domain,
            preg_match("@^www\\..+(\\.co\\.uk|\\.com)$@", $domain)
        );
    }
?>

From what I understand from your question, the domain needs to start with www AND ends with either .co.uk OR .com. So here is the RegExp:

<?php
    $domains = array(
        "www.google.com",
        "www.google.co.uk",
        "google.com"
    );
    foreach($domains as $domain){
        echo sprintf(
            "%20s -> %d\n",
            $domain,
            preg_match("@^www\\..+(\\.co\\.uk|\\.com)$@", $domain)
        );
    }
?>
甜心小果奶 2024-10-16 22:36:42

简单

$ar = array();
$t = array("www.google.com","www.google.co.uk","google.com","www.google");
foreach ($t as $str) {
    if (preg_match('/^www\.(.*)(\.co.uk|\.com)$/',$str)) {
        echo "Matches\n";
    }
}

条件

$ar = array();
$t = array("www.google.com","www.google.co.uk","google.com","www.google");
foreach ($t as $str) {
    switch (true) {
        case preg_match('/^www\.(.*)$/',$str) :
        // code
        case preg_match('/^(.*)(\.co.uk)$/',$str) :
        // code
        case preg_match('/^(.*)(\.com)$/',$str) :
        // code
        break;
    }
}

Simple

$ar = array();
$t = array("www.google.com","www.google.co.uk","google.com","www.google");
foreach ($t as $str) {
    if (preg_match('/^www\.(.*)(\.co.uk|\.com)$/',$str)) {
        echo "Matches\n";
    }
}

Conditional

$ar = array();
$t = array("www.google.com","www.google.co.uk","google.com","www.google");
foreach ($t as $str) {
    switch (true) {
        case preg_match('/^www\.(.*)$/',$str) :
        // code
        case preg_match('/^(.*)(\.co.uk)$/',$str) :
        // code
        case preg_match('/^(.*)(\.com)$/',$str) :
        // code
        break;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文