2017-03-15 12 views
1

メタタグから名前を取得しようとしています。php URLからURLタグを取得する方法

私はこのようなメタタグを取得:その後、

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

を:

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

私は説明を持っていますが、なし:

meta name="description 

どのように私はこれを得ることができますか?

答えて

2

get_meta_tagsは、ここで追加のHTMLタグや情報

なしのみのメタデータを取得していますが、あなたのコードに必要な方法を使用して任意のWebページに関するすべてのメタ情報を取り出すコードです:

あなたのを取得する必要があります$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); 
+1

するには、[OK]おかげで、私はあなたを試してみました望む方法を使用することができます@メタ良いコード:) – Ygg69

+0

@ ShaH:どのように私はカテゴリーのようなすべてのMETAタグを取得できますか? – Rathinam

+1

@Rathinam私は上記のコードが同じコードに答えていると思います。ローカルシステムのコードをコピーしてファイルを参照してチェックするか、phpfiddleにコードを貼り付けて、作成者、説明、キーワードなどのカテゴリの賢明なメタタグを表示してください。 – ShaH

関連する問題