[英]Get other part of array when match found
這是我正在使用的正則表達式。 它工作正常,但現在我正在嘗試獲得結果。
基本上,如果名稱/屬性/等包含“標題”,我希望它回顯標題元標記的內容。
換句話說,當out [1]包含“ title”(不區分大小寫)時,我想要對應的out [2],而不是out [1]。
$pattern = '
~<\s*meta\s
# using lookahead to capture type to $1
(?=[^>]*?
\b(?:name|property|http-equiv)\s*=\s*
(?|"\s*([^"]*?)\s*"|\'\s*([^\']*?)\s*\'|
([^"\'>]*?)(?=\s*/?\s*>|\s\w+\s*=))
)
# capture content to $2
[^>]*?\bcontent\s*=\s*
(?|"\s*([^"]*?)\s*"|\'\s*([^\']*?)\s*\'|
([^"\'>]*?)(?=\s*/?\s*>|\s\w+\s*=))
[^>]*>
~ix';
if(preg_match_all($pattern, $link_html, $out))
{
foreach ($out[1] as $out)
{
echo $out.'<br>';
}
}
您要求使用正則表達式,但是使用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
XPath意味着找到任何元標記的所有內容屬性,其中任何屬性名稱都是字符串“ name | property | http-equiv”的一部分,並且在該屬性中包含值“ title” 。
正如您希望看到的那樣,XPath本身幾乎讀起來就像是自然語言(與您使用的正則表達式相反)。
這可以通過在foreach
循環中捕獲數組索引來實現,如下所示:
foreach ($out[1] as $index => $out) {
if(stristr($out, 'title')) echo $out[2][$index].'<br>';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.