繁体   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