get_meta_tags

(PHP 3 >= 3.0.4, PHP 4, PHP 5)

get_meta_tags --  從一個檔案中提取所有的 meta 標籤 content 屬性,返回一個陣列

描述

array get_meta_tags ( string filename [, int use_include_path] )

開啟 filename 逐行解析檔案中的 <meta> 標籤。此參數可以是本地檔案也可以是一個 URL。解析工作將在 </head> 處停止。

use_include_path 設定為 1 將促使 PHP 嘗試按照 include_path 標準包括路徑中的每個指向去開啟檔案。這只用於本地檔案,不適用於 URL。

例子 1. get_meta_tags() 解析些什麼

<meta name="author" content="name">
<meta name="keywords" content="php documentation">
<meta name="DESCRIPTION" content="a php manual">
<meta name="geo.position" content="49.33;-86.59">
</head> <!-- 解析工作在此處停止 -->
(注意換行換行 - PHP 使用一個本地函數來解析輸入,所以 Mac 上的檔案將不能在 Unix 上標準工作)。

返回的關聯陣列以屬性 name 的值作為鍵,屬性 content 的值作為值,所以你可以很容易地使用標準陣列函數遍歷此關聯陣列或訪問某個值。屬性 name 中的特殊字元將使用『_』置換,而其它字元則轉換成小寫。若果有兩個 meta 標籤擁有相同的 name,則只返回最後出現的那一個。

例子 2. get_meta_tags() 的返回值

<?php
// 假設上邊的標籤是在 www.example.com 中
$tags get_meta_tags('http://www.example.com/');

// 注意所有的鍵(key)均為小寫,而鍵中的『.』則轉換成了『_』。
print $tags['author'];       // name
print $tags['keywords'];     // php documentation
print $tags['description'];  // a php manual
print $tags['geo_position']; // 49.33;-86.59
?>

注: 從 PHP 4.0.5 開始,get_meta_tags() 支援沒有使用引號括起來的 HTML 屬性。

參見 htmlentities()urlencode()