Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正则表达式来减少url中的子域_Php_Regex_Preg Match_Preg Match All - Fatal编程技术网

Php 正则表达式来减少url中的子域

Php 正则表达式来减少url中的子域,php,regex,preg-match,preg-match-all,Php,Regex,Preg Match,Preg Match All,我有一个regexp,它与以下内容匹配:wiseman.google.com.jp、me.co.uk、paradise.museum、abcd-abc.net、www.google.jp、12345-daswe-23dswe-dswedswe-54eddss.info、del.icio.us、jo.ggi.ng,所有这些都来自一个textarea值 使用regexp(在preg_match_all($regex1,$str,$match)中)来获取上述值:/(?:[a-zA-Z0-9]{2,3}

我有一个regexp,它与以下内容匹配:wiseman.google.com.jp、me.co.uk、paradise.museum、abcd-abc.net、www.google.jp、12345-daswe-23dswe-dswedswe-54eddss.info、del.icio.us、jo.ggi.ng,所有这些都来自一个textarea值

使用regexp(在preg_match_all($regex1,$str,$match)中)来获取上述值:
/(?:[a-zA-Z0-9]{2,3}.)?[-a-zA-Z0-9]{2,7}.[a-zA-Z0-9]{2,7}./

现在,我的问题是:如何让regexp将“wiseman.google.com.jp”缩减为“google.com.jp”,将“www.google.jp”缩减为“google.jp”

我想做第二个preg_match($regex2,$str,$match)函数,每个值都来自preg_match_all函数

我在$regex2:
([-a-zA-Z0-9\x{0080}-\x{00FF}]{2,}+.[a-zA-Z0-9\x{0080}-\x{00FF}{2,7}(?:\.[-a-zA-Z0-9\x{0080}-\x{00FF}{2,3})中尝试了这个regexp

有什么意见吗?短暂性脑缺血发作

以下是我的小解决方案:

preg_match_all($regex, $str, $matches, PREG_PATTERN_ORDER);
$arrlength=count($matches[0]);
for($x=0;$x<$arrlength;$x++){
    $dom = $matches[0][$x];
    $newstringcount = substr_count($dom, '.');   // this line is to count how many "." present in the string.
    if($newstringcount == 3){                       // if there are 3 '.' present in the string = true
        $pos = strpos($dom, '.', 0);              // this line is to find the first occurence of the '.' in the string
        $find = substr($dom, $pos+1);            //this line is to get the value after the first occurence of the '.' in the string
        echo $find;
    }else if($newstringcount == 2){
        if ($pos = strpos($dom,'www.') !== false) {
            $find = substr($dom, $pos+3);
            echo $find;
        }else{
            echo $dom;
        }
    }else if($newstringcount == 1){
        echo $dom;
    }
    echo "<br>";
}
preg_match_all($regex,$str,$matches,preg_PATTERN_ORDER);
$arrlength=计数($matches[0]);
对于($x=0;$x)(注意:只有在您必须使用正则表达式或有点……绝望的情况下,此答案才适合您的需要)

由于像
.com.jp
.co.uk
这样的域,您想要实现的目标在一般规则下是不可能实现的 人们能找到的唯一一般规则是:

从右向左读取时,有一个或两个TLD,后跟一个二级域

因此,我们必须将所有可用的TLD列为白名单。我想我将以下内容称为“域kraken
释放海怪

将其与
i
m
标志一起使用。 这假设您的数据在多行上

如果数据由
分隔,请将正则表达式中的最后一个字符(
$
)更改为
,?
,并使用
g
i
标志

演示可在和上获得。
(两个演示都有一个解释:regex101用文本描述它,而debuggex则可视化beast)


可用TLD的列表可在上找到,正则表达式中使用的TLD从2014年1月开始。

如果需要拆分它…那么第一次出现点的简单substr如何?()您想通过使用regexp?或任何方法来实现这一点吗?嘿@Goikiu我已经解决了这个问题,但它需要进一步完善。稍后将发布我的解决方案。谢谢。下面是代码:
$arrlength=count($matches[0]);for($x=0;$xAs这有点不可读,您应该将此代码编辑到您的问题中(在您问题下方的标签下有一个小链接<代码>编辑)。哈哈哈,是的,我非常想学习。我只是想知道是否有解决方案。我还考虑了tld的列表。感谢您的输入和链接。:)但请不要在生产代码中使用它;)我这样做只是为了好玩…这只会破坏你的表现…坚持你的解决方案:)
([a-z0-9\-]{2,63}(?:\.(?:a(?:cademy|ero|rpa|sia|[cdefgilmnoqrstuwxz])|b(?:ike
|iz|uilders|uzz|[abdefghijlmnoqrstvwyz])|c(?:ab|amera|amp|areers|at|enter|eo
|lothing|odes|offee|om(?:pany|puter)?|onstruction|ontractors|oop|
[acdfghiklmnoruvwxyz])|d(?:iamonds|irectory|omains|[ejkmoz])|e(?:du(?:cation)?
|mail|nterprises|quipment|state|[ceghrstu])|f(?:arm|lorist|[ijkmor])|g(?:allery|
lass|raphics|uru|[abdefghlmnpqrstuwy])|h(?:ol(?:dings|iday)|ouse|[kmnrtu])|
i(?:mmobilien|n(?:fo|stitute|ternational)|[delmnoqrst])|j(?:obs|[emop])|
k(?:aufen|i(?:tchen|wi)|[eghimnprwxyz])|l(?:and|i(?:ghting|mo)|[abcikrstuvy])|
m(?:anagement|enu|il|obi|useum|[acdefghklmnopqrstuvwxyz])|n(?:ame|et|inja|
[acefgilopruz])|o(?:m|nl|rg)|p(?:hoto(?:graphy|s)|lumbing|ost|ro|[aefghklmnrstwy])|
r(?:e(?:cipes|pair)|uhr|[eosuw])|s(?:exy|hoes|ingles|ol(?:ar|utions)|upport|
ystems|[abcdeghijklmnorstuvxyz])|t(?:attoo|echnology|el|ips|oday|
[cdfghjklmnoprtvwz])|u(?:no|[agkmsyz])|v(?:entures|iajes|oyage|[aceginu])|
w(?:ang|ien|[fs])|xxx|y(?:[et])|z(?:[amw]))){1,2})$