Wednesday, August 6, 2014

extract meta tag content in php

8/06/2014

In last post we add script about grab webpage title tag and description , keywords meta tag content. But now this post we explain that how programmer can extract all the meta tags information from specific webpage with get_meta_tags and returns an array.
 


<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.com/
*/


function get_meta_data($url, $searchkey='') {   
    $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 .= '

' . $key . ' (' . count($array) . ' words | ' . strlen($value) . ' chars)' . $value . '

'; // format data with count of words and chars } } return $str; } ?>
How you can use get_meta_data
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.com/
*/

$content = get_meta_data("http://4evertutorials.blogspot.com/"); 

echo "
";
print_r($content);
echo "
"; ?>

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top