PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<get_headershttp_build_query>
Last updated: Mon, 16 Jul 2012

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




<get_headershttp_build_query>
 Last updated: Mon, 16 Jul 2012
 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://manual.phpv.net/
Last updated: Thu Jul 7 19:13:47 2005 CST