简体   繁体   中英

Php how get meta tags name from url page

i try to get name from meta tags.

I get meta tags like this :

$test = get_meta_tags('http://viedemerde.fr');

then :

echo $test['description'];
...

I have the description, but without :

meta name="description

How can i get this ?

get_meta_tags Does fetch only meta data without additional html tags or info

Here is a code that fetches all meta and information about any webpage use the way you want for your code:

You should get your meta @ $test["metaTags"]["description"]["html"];

in fallowing code or you can use it the way you would want to

function getUrlData($url) {
$result = false;

$contents = getUrlContents($url);

if (isset($contents) && is_string($contents)) {
    $title = null;
    $metaTags = 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="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);

    if (isset($match) && is_array($match) && count($match) == 3) {
        $originals = $match[0];
        $names = $match[1];
        $values = $match[2];

        if (count($originals) == count($names) && count($names) == count($values)) {
            $metaTags = array();

            for ($i = 0, $limiti = count($names); $i < $limiti; $i++) {
                $metaTags[$names[$i]] = array(
                    'html' => htmlentities($originals[$i]),
                    'value' => $values[$i]
                );
            }
        }
    }

    $result = array(
        'title' => $title,
        'metaTags' => $metaTags
    );
}
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;}

$test = getUrlData('http://ocsidtechnologies.com');  //Replace  with your URL here
echo '<pre>';print_r($test);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM