【问题标题】:RegEx, preg_match_all and filter domainsRegEx、preg_match_all 和过滤域
【发布时间】:2013-11-14 09:24:38
【问题描述】:

几个小时以来,我一直在尝试执行我的公平脚本并最终几乎准备就绪,但它出现了奇怪的问题。

附上一段代码以了解我的意思。

$sep = '\.com|\.tv';
$string = 'sub.sub2.sub3.tv-bole-el.com';
$pat = '~[-[:alnum:]]{2,}\b('.$sep.')\b~i';

preg_match_all($pat, $string, $matches, PREG_PATTERN_ORDER);

在这种情况下,在数组 $matches 中,如果您花费 tv-bole-el.com 将是正确的

相反,我获得了 2 个域名:

  • sub3.tv
  • -bole-el.com

有人知道哪里错了吗?

【问题讨论】:

  • 您的预期输出究竟是什么?
  • 预期输出:tv-bole-el.com
  • 那么为什么你的$sep 包含.tv 作为一个有效的结尾?
  • 如果将$ 锚添加到正则表达式的末尾会怎样?
  • @NiettheDarkAbsol,因为如果我想从大列表中捕获 .tv 域,我需要它。

标签: php regex preg-match-all


【解决方案1】:

不确定这是否是您需要的,但这对我有用:

$sep = '\.com|\.tv';
$string = 'sub.sub2.sub3.tv-bole-el.com';
$pat = '~[-[:alnum:]]{2,}\b('.$sep.')$~i';
preg_match_all($pat, $string, $matches, PREG_PATTERN_ORDER);

var_dump($matches[0]);

输出:

array(1) {
  [0]=>
  string(14) "tv-bole-el.com"
}

【讨论】:

  • 我试了一下,但是放了之后:$string='subdomain1.subdomain2.subdomain3.tv-a-borla.com 7days.com';输出其: [0] => Array ( [0] => 7days.com ) [1] => Array ( [0] => .com )
猜你喜欢
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多