Php 找到匹配项时获取数组的其他部分

Php 找到匹配项时获取数组的其他部分,php,arrays,regex,title,Php,Arrays,Regex,Title,这是和我一起工作的正则表达式。它工作得很好,但现在我正在努力得到结果 基本上,如果name/property/etc包含“title”,我希望它能够响应title元标记的内容 换句话说,当out[1]包含“title”(不区分大小写)时,我需要对应的out[2],而不是out[1] $pattern= ~]*? \b(?:名称|属性| http等价)\s*=\s* (?|“\s*([^”]*?)\s*“\'\s*([^\']*?)\s*'| ([^“\'>]*?)(?=\s*/?\s*>\s\w

这是和我一起工作的正则表达式。它工作得很好,但现在我正在努力得到结果

基本上,如果name/property/etc包含“title”,我希望它能够响应title元标记的内容

换句话说,当out[1]包含“title”(不区分大小写)时,我需要对应的out[2],而不是out[1]

$pattern=
~]*?
\b(?:名称|属性| http等价)\s*=\s*
(?|“\s*([^”]*?)\s*“\'\s*([^\']*?)\s*'|
([^“\'>]*?)(?=\s*/?\s*>\s\w+\s*=))
)
#捕获内容至$2
[^>]*?\b内容\s*=\s*
(?|“\s*([^”]*?)\s*“\'\s*([^\']*?)\s*'|
([^“\'>]*?)(?=\s*/?\s*>\s\w+\s*=))
[^>]*>
~ix';
if(preg_match_all($pattern,$link_html,$out))
{
foreach($out[1]作为$out)
{
回显$out。“
”; } }
这应该可以通过在
foreach
循环中捕获数组索引来实现,如下所示:

foreach ($out[1] as $index => $out) {
    if(stristr($out, 'title')) echo $out[2][$index].'<br>';
}
foreach($out[1]作为$index=>$out){
if(stristr($out,'title'))回显$out[2][$index]。
'; }
您要求使用正则表达式,但使用HTML解析器和XPath将更容易阅读:

<?php

$html = <<< HTML
<html>
    <head>
        <meta name="author" lang="en" content="Gordon" />
        <meta name="title" lang="en" content="match this" />
        <meta property="title" lang="en" content="and this" />
        <meta http-equiv="title" lang="en" content="and also this" />
        <meta foo="title" content="but not this" />
    </head>
    <body>Use DOMDocument for HTML parsing instead</body>
</html>
HTML;

libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($html);
libxml_use_internal_errors(false);

$xpath = new DOMXPath($dom);
$nodes = $xpath->evaluate(
    '//meta[
       @*[
         contains("name|property|http-equiv", name())
         and contains(., "title")
         ]
       ]/@content'
);

foreach ($nodes as $node) {
    echo $node->nodeValue, PHP_EOL;
}
XPath意味着查找任何元标记的所有内容属性,其中任何属性名称都是字符串“name | property | http equiv”的一部分,并且在该属性中包含值“title”


正如您所希望看到的,XPath本身的读取方式几乎就像是自然语言一样(与您使用的正则表达式相反)

为什么不改用html解析器呢?噢,哇,我很久以前就用过XPath,后来就忘了……你说得对……如果允许的话,我会接受。。。。谢谢!:)
<?php

$html = <<< HTML
<html>
    <head>
        <meta name="author" lang="en" content="Gordon" />
        <meta name="title" lang="en" content="match this" />
        <meta property="title" lang="en" content="and this" />
        <meta http-equiv="title" lang="en" content="and also this" />
        <meta foo="title" content="but not this" />
    </head>
    <body>Use DOMDocument for HTML parsing instead</body>
</html>
HTML;

libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($html);
libxml_use_internal_errors(false);

$xpath = new DOMXPath($dom);
$nodes = $xpath->evaluate(
    '//meta[
       @*[
         contains("name|property|http-equiv", name())
         and contains(., "title")
         ]
       ]/@content'
);

foreach ($nodes as $node) {
    echo $node->nodeValue, PHP_EOL;
}
match this
and this
and also this