Last active
December 25, 2015 11:18
-
-
Save yyaremenko/6967325 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Attempts to parse given meta tag's content from given html; | |
* deals with 'standard' and Open Graph meta tags. | |
* | |
* Order of meta tag attributes could be swapped around e.g. can either be | |
* name="..." content="..." | |
* or | |
* content="..." name="..." | |
* | |
* so we try to get meta tag content in 'swapped' way | |
* if we don't get in 'straight' | |
* | |
* @param string $html | |
* - html, which contains meta tags | |
* @param string $tagName | |
* - name of meta tag, e.g. 'description' | |
* @return null | |
*/ | |
function getMetaTag($html, $tagName) { | |
$matches = array(); | |
preg_match('/<meta.*?(name|property)=("|\')(og:)?' . $tagName . '("|\').*?content=("|\')(.*?)("|\')/i', $html, $matches); | |
if(isset($matches[6])) { | |
return trim($matches[6]); | |
} | |
preg_match('/<meta.*?content=("|\')(.*?)("|\').*?(name|property)=("|\')(og:)?' . $tagName . '("|\')/i', $html, $matches); | |
if(isset($matches[2])) { | |
return trim($matches[2]); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment