Showing posts with label meta tags. Show all posts
Showing posts with label meta tags. Show all posts

Wednesday, August 6, 2014

extract meta tag content in php

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 "
"; ?>

Monday, August 4, 2014

get title, meta tags info in php

This php script grab title tag and description, keywords meta of specified url with simple php curl function. And show all information in title tag , meta description and meta keywords tags.

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

function file_get_contents_curl($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

$html = file_get_contents_curl("http://4evertutorials.blogspot.com/");

$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');
}

echo "Title: $title". '

';
echo "Description: $description". '

';
echo "Keywords: $keywords";





?>

 

© 2014 4everTutorials. All rights resevered.

Back To Top