note 117766 added to function.get-meta-tags
| From: | bobblebubble | Date: | Wed, 05 Aug 2015 20:07:31 +0000 |
| Subject: | note 117766 added to function.get-meta-tags | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203267@lists.php.net to get a copy of this message | ||
This regex gets meta tags independent of sequence by capturing inside a lookahead.
Further uses the branch reset feature for different quote styles of values.
The pattern can be tested here: https://regex101.com/r/oE4oU9/1
<?PHP
function getMetaTags($str)
{
$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, $str, $out))
return array_combine($out[1], $out[2]);
return array();
}
// usage
$meta_tags = getMetaTags($str);
?>
----
Server IP: 72.52.91.14
Probable Submitter: 183.88.79.108
----
Manual Page -- http://php.net/manual/en/function.get-meta-tags.php
Edit -- https://master.php.net/note/edit/117766
Del: integrated -- https://master.php.net/note/delete/117766/integrated
Del: useless -- https://master.php.net/note/delete/117766/useless
Del: bad code -- https://master.php.net/note/delete/117766/bad+code
Del: spam -- https://master.php.net/note/delete/117766/spam
Del: non-english -- https://master.php.net/note/delete/117766/non-english
Del: in docs -- https://master.php.net/note/delete/117766/in+docs
Del: other reasons-- https://master.php.net/note/delete/117766
Reject -- https://master.php.net/note/reject/117766
Search -- https://master.php.net/manage/user-notes.php