簡體   English   中英

PHP如何從URL頁面獲取元標記名稱

[英]Php how get meta tags name from url page

我嘗試從meta標簽獲取名稱。

我得到這樣的元標記:

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

然后 :

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

我有描述,但沒有:

meta name="description

我怎么能得到這個?

get_meta_tags僅獲取元數據,而沒有其他html標簽或信息

這是一個代碼,該代碼使用您想要的代碼方式獲取有關任何網頁的所有元數據和信息:

您應該獲得元@ $test["metaTags"]["description"]["html"];

在休閑代碼中,或者您可以按照自己想要的方式使用它

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM