note 117176 added to function.get-meta-tags

From: Date: Sat, 25 Apr 2015 20:38:09 +0000
Subject: note 117176 added to function.get-meta-tags
Groups: php.notes 
Request: Send a blank email to php-notes+get-202373@lists.php.net to get a copy of this message
New version based on mariano at cricava dot com's work with: 1) Support for Meta properties (like Facebook's og tags). 2) Support for Unicode (UTF-8) encoded Meta lines. 3) An option not to convert htmlentities - if you plan to actually use the results and not just display them. function getUrlData($url, $raw=false) // $raw - enable for raw display { $result = false; $contents = getUrlContents($url); if (isset($contents) && is_string($contents)) { $title = null; $metaTags = null; $metaProperties = null; preg_match('/<title>([^>]*)<\/title>/si', $contents, $match ); if (isset($match) && is_array($match) && count($match) > 0) { $title = strip_tags($match[1]); } preg_match_all('/<[\s]*meta[\s]*(name|property)="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match); if (isset($match) && is_array($match) && count($match) == 4) { $originals = $match[0]; $names = $match[2]; $values = $match[3]; if (count($originals) == count($names) && count($names) == count($values)) { $metaTags = array(); $metaProperties = $metaTags; if ($raw) { if (version_compare(PHP_VERSION, '5.4.0') == -1) $flags = ENT_COMPAT; else $flags = ENT_COMPAT | ENT_HTML401; } for ($i=0, $limiti=count($names); $i < $limiti; $i++) { if ($match[1][$i] == 'name') $meta_type = 'metaTags'; else $meta_type = 'metaProperties'; if ($raw) ${$meta_type}[$names[$i]] = array ( 'html' => htmlentities($originals[$i], $flags, 'UTF-8'), 'value' => $values[$i] ); else ${$meta_type}[$names[$i]] = array ( 'html' => $originals[$i], 'value' => $values[$i] ); } } } $result = array ( 'title' => $title, 'metaTags' => $metaTags, 'metaProperties' => $metaProperties, ); } return $result; } function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0) { $result = false; $contents = @file_get_contents($url); // Check if we need to go somewhere else if (isset($contents) && is_string($contents)) { preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $contents, $match); if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1) { if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections) { return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection); } $result = false; } else { $result = $contents; } } return $contents; } ?> <?php $result = getUrlData('http://whatever...', true); echo '<pre>'; print_r($result, true); echo '</pre>'; ?> Output example: <?php Array ( [title] => The requested page's title [metaTags] => Array ( [description] => Array ( [html] => <meta name="description" content="Something..." /> [value] => Something... ) ) [metaProperties] => Array ( [og:type] => Array ( [html] => <meta property="og:type" content="article"/>/> [value] => article ) ) ) ?> ---- Server IP: 72.52.91.14 Probable Submitter: 96.31.64.186 ---- Manual Page -- http://php.net/manual/en/function.get-meta-tags.php Edit -- https://master.php.net/note/edit/117176 Del: integrated -- https://master.php.net/note/delete/117176/integrated Del: useless -- https://master.php.net/note/delete/117176/useless Del: bad code -- https://master.php.net/note/delete/117176/bad+code Del: spam -- https://master.php.net/note/delete/117176/spam Del: non-english -- https://master.php.net/note/delete/117176/non-english Del: in docs -- https://master.php.net/note/delete/117176/in+docs Del: other reasons-- https://master.php.net/note/delete/117176 Reject -- https://master.php.net/note/reject/117176 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202373) next »