匹配两个可能的 rel 值<link>

  • 本文关键字:rel link 两个 php preg-match
  • 更新时间 :
  • 英文 :


我目前正在使用以下代码解析webabout端点。这适用于<link rel="webmention" href=" "><link rel="http://webmention.org/" href=" ">,但如果两者都包括,即<link rel="webmention http://webmention.org/" href=" ">,则不适用。我正在努力使它适应这种情况。当前代码:

if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="webmention"[ ]*/?>/i', $body, $match)
  || preg_match('/<(?:link|a)[ ]+rel="webmention"[ ]+href="([^"]+)"[ ]*/?>/i', $body, $match)) {
    $endpoint = $match[1];
} elseif(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="http://webmention.org/?"[ ]*/?>/i', $body, $match)
  || preg_match('/<(?:link|a)[ ]+rel="http://webmention.org/?"[ ]+href="([^"]+)"[ ]*/?>/i', $body, $match)) {
    $endpoint = $match[1];
}

有什么想法吗?

我刚刚写了这篇文章,看看你是否觉得它有用

'/<link.+?rel=["']?(?:webmention|http://webmention.org/?)['"]?.*?>/g'

演示

附言:一句忠告——你用正则表达式就像它们什么都不是一样。只有在没有其他方法的情况下,才应该使用正则表达式,尤其是如果$body是一个大字符串,你真的不应该在它上运行这么多preg_match。干杯!

最新更新