How to get the Title, Meta Information of a URL
Nov-17-2014 in PHP No comments
Do you want to figure out how to get the title, meta information( include: description, keywords, author,….) of a Url?
There are many ways to do it but here I will guide you to do that is very simple by the use of function get_meta_tags(). This function get_meta_tags() will help you with get all meta information but not the title. To get the title of url you just use following function.
1 2 3 4 5 6 7 8 |
<?php function pageTitle($page_url) { preg_match("/<title>(.+)<\/title>/siU", file_get_contents($page_url), $matches); $title = $matches[1]; echo $title; } ?> |
Here below shows the PHP code to get all the meta information form the URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php function get_meta_data($url, $searchkey='') { if ($data = @get_meta_tags($url)) { $data = @get_meta_tags($url); // get the meta data in an array foreach($data as $key => $value) { if(mb_detect_encoding($value, 'UTF-8, ISO-8859-1', true) != 'ISO-8859-1') { // check whether the content is UTF-8 or ISO-8859-1 $value = utf8_decode($value); // if UTF-8 decode it } $value = strtr($value, get_html_translation_table(HTML_ENTITIES)); // mask the content if($searchkey != '') { // if only one meta tag is in demand e.g. 'description' if($key == $searchkey) { $str = $value; // just return the value } } else { // all meta tags $pattern = '/ ¦,/i'; // ' ' or ',' $array = preg_split($pattern, $value, -1, PREG_SPLIT_NO_EMPTY); // split it in an array, so we have the count of words $str .= '<tr><th class=\'resultTable\'>' . $key . '(' . count($array) . ' words ¦ ' . strlen($value) . ' chars)</th></tr> <tr><td class=\'row1\'>' . $value . '</td></tr>'; // format data with count of words and chars } } return $str; }else { // Catch error echo 'Not display meta tags!'; } } ?> |
How to use this function?
1 2 3 4 |
<?php $url = "visofts.com"; echo get_meta_data($url); ?> |
By using the above code when you posts a url, this script should retrieve the title and the meta tags.
See Demo Meta Tags extractor