【问题标题】:PHP preg_match() not working [duplicate]PHP preg_match()不起作用[重复]
【发布时间】:2015-05-06 23:56:40
【问题描述】:

我编写了一个脚本,用于扫描多个网站以查找 google 链接以确保其存在。出于某种原因,我的脚本无法正常工作。当我在http://www.regexr.com/ 检查它时,它可以工作,但不能在实时实现中使用。

应该找到的链接示例:

https://plus.google.com/+VincentsHeatingPlumbingIncPortHuronTownship/about?hl=en

preg_match 我正在使用:

if (preg_match_all("/(.*)plus.google.com(.*)/", $attributeValue->value)) 
{ 
  $googleLinkCount ++;
  $googleLinkHrefs[] = $attributeValue->value;
}

【问题讨论】:

  • 仍然不能使用 preg_match_all 而不是 g 修饰符,所以谢谢,但不是这样。
  • 切换到m (PCRE_MULTILINE) 而不是g 修饰符
  • 下面给出了正确答案,但感谢所有想要关注非问题的人。

标签: php preg-match


【解决方案1】:

不要使用正则表达式,使用parse_url:

if (parse_url($attributeValue->value, PHP_URL_HOST) === 'plus.google.com') {
    // host is plus.google.com
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    相关资源
    最近更新 更多