+/**
+ * Extract meta tag from HTML content in either:
+ * - OpenGraph: <meta property="og:[tag]" ...>
+ * - Meta tag: <meta name="[tag]" ...>
+ *
+ * @param string $tag Name of the tag to retrieve.
+ * @param string $html HTML content where to look for charset.
+ *
+ * @return bool|string Charset string if found, false otherwise.
+ */
+function html_extract_tag($tag, $html)
+{
+ $propertiesKey = ['property', 'name', 'itemprop'];
+ $properties = implode('|', $propertiesKey);
+ // Try to retrieve OpenGraph image.
+ $ogRegex = '#<meta[^>]+(?:'. $properties .')=["\']?(?:og:)?'. $tag .'["\'\s][^>]*content=["\']?(.*?)["\'/>]#';
+ // If the attributes are not in the order property => content (e.g. Github)
+ // New regex to keep this readable... more or less.
+ $ogRegexReverse = '#<meta[^>]+content=["\']([^"\']+)[^>]+(?:'. $properties .')=["\']?(?:og)?:'. $tag .'["\'\s/>]#';
+
+ if (preg_match($ogRegex, $html, $matches) > 0
+ || preg_match($ogRegexReverse, $html, $matches) > 0
+ ) {
+ return $matches[1];
+ }
+
+ return false;
+}
+