php get_headers 与get_meta_tags函数实例

 更新时间:2016年11月25日 17:11  点击:2142
get_headers是获取http头信息,get_meta_tags是获取网页的description,abstract,keywords等信息


*/
$url='http://www.111cn.net';          //定义url
print_r(get_headers($url));         //无格式获取
print_r(get_headers($url,1));         //带上格式获取


/*

array
(
    [0] => http/1.1 200 ok
    [1] => connection: close
    [2] => date: sat, 11 dec 2010 05:43:46 gmt
    [3] => content-length: 69617
    [4] => content-type: text/html
    [5] => content-location: http://www.111cn.net/

    [6] => last-modified: sat, 11 dec 2010 05:19:51 gmt
    [7] => accept-ranges: bytes
    [8] => etag: "e8be4b9f398cb1:6c66"
    [9] => server: microsoft-iis/6.0
    [10] => x-powered-by: asp教程.net
)
array
(
    [0] => http/1.1 200 ok
    [connection] => close
    [date] => sat, 11 dec 2010 05:43:47 gmt
    [content-length] => 69617
    [content-type] => text/html
    [content-location] => http://www.111cn.net/index.html
    [last-modified] => sat, 11 dec 2010 05:19:51 gmt
    [accept-ranges] => bytes
    [etag] => "e8be4b9f398cb1:6c66"
    [server] => microsoft-iis/6.0
    [x-powered-by] => asp.net教程
)

get_meta_tags($url);
获取指定网页的description,abstract,keywords等信息
*/

$tags=get_meta_tags('http://www.111cn.net/');      //提取meta标签
print_r($tags)            //输出数组全部内容

/*

array
(
    [description] => 网页制作教程网提供专业网页设计教程与网页制作教程包括有photoshop教程,flash,html,css教程,dreamweaver,fireworks,asp,php教程,jsp教程,asp.net,网站建设,网站开发,网页特效,平面设计,个人网站,网页素材
    [abstract] => 提供最新的网页制作教程、网页设计教程、网页特效,为个人网站提供网页素材模板和网页视频学习。
    [keywords] => 网页设计教程,网页制作教程,网页学习,photoshop,flash,html,css,dreamweaver,fireworks,asp,php,jsp,asp.net,网页特效,平面设计,网页素材
)

注意:使用该函数需要把 php.ini里面的allow_url_fopen = on,才能使用

<?php教程


//连接数据库教程
$link = mysql教程_connect('localhost','root','密码') or die(mysql_error());
mysql_select_db('sortclass',$link);
mysql_query("set names 'gbk'");
//无限分类类库
class sortclass{

var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
var $link;
var $table;
function sortclass($link, $table){
$this->setnode(0, -1, '顶极节点');
$this->link = $link;
$this->table = $table;
$node = array();
$results = mysql_query('select * from '.$this->table.'',$this->link);
while($node = mysql_fetch_assoc($results)){
$this->setnode($node['cid'],$node['pid'],$node['cname']);
}
}
function setnode ($id, $parent, $value){
$parent = $parent?$parent:0;
$this->data[$id] = $value;
$this->child[$id] = array();
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
$this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
}
function getlist (&$tree, $root= 0){
foreach ($this->child[$root] as $key=>$id){
$tree[] = $id;
if ($this->child[$id]) $this->getlist($tree, $id);
}
}
function getvalue ($id){return $this->data[$id];}
function getlayer ($id, $space = false){
return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
}
function getparent ($id){return $this->parent[$id];}
function getparents ($id){
while ($this->parent[$id] != -1){
$id = $parent[$this->layer[$id]] = $this->parent[$id];
}
ksort($parent);
reset($parent);
return $parent;
}
function getchild ($id){return $this->child[$id];}
function getchilds ($id = 0){
$child = array($id);
$this->getlist($child, $id);
return $child;
}
function addnode($name,$pid){
mysql_query("insert into $this->table (`pid`,`cname`) values ('$pid','$name')",$this->link);
}
function modnode($cid, $newname){
mysql_query("update $this->table set `cname`='$newname' where `cid` = $cid",$this->link);
}
function delnode($cid){
$allchilds = $this->getchilds($cid);
$sql ='';
if(empty($allchilds)){
$sql = "delete from $this->table where `cid` = $cid";
}else{
$sql = 'delete from '.$this->table.' where `cid` in ('.implode(',',$allchilds).','.$cid.')';
}
mysql_query($sql,$this->link);
}
function movenode($cid, $topid){
mysql_query("update $this->table set `pid`=$topid where `cid` = $cid", $this->link);
}
}
//函数
function back(){
echo '<script language="网页特效">window.location.href="class.php?"+new date().gettime();</script>';
exit;
}
//生成select
function makeselect($array,$formname){
global $tree;
$select = '<select name="'.$formname.'">';
foreach ($array as $id){
$select.='<option value="'.$id.'">'.$tree->getlayer($id, '|-').$tree->getvalue($id)."</option>";
}
return $select.'</select>';
}
$tree = new sortclass($link,'`class`');
$op = !empty($_post['op']) ? $_post['op'] : $_get['op'];
if(!empty($op)){

if($op=='add'){
$tree->addnode($_post['cname'],$_post['pid']);
back();
}

if($op=='mod'){
$tree->modnode($_post['cid'],$_post['cname']);
back();
}

if($op=='del'){
$tree->delnode($_get['cid']);
back();
}

if($op=='move'){
$tree->movenode($_post['who'],$_post['to']);
back();
}
}
$category = $tree->getchilds();
?>
<style type="text/css教程">
body{font-size:12px;}
ul{list-style:none;}
a{cursor:pointer;}
input{ margin-top:8px;}
</style>
<script language="javascript">
function $(e){return document.getelementbyid(e);}
function mod(cid){
$('cid').value=cid;
$('op').value='mod';
$('name').style.border='1px solid red';
}
</script>
<h3>添加分类</h3>
<form action="class.php" method="post">
名称:<input type="text" id="name" name="cname" /> 添加到:<?=makeselect($category,'pid')?><br />
<input type="hidden" id="op" name="op" value="add" />
<input type="hidden" id="cid" name="cid" />
<input type="submit" value="添加分类" />
</form>
<h3>移动分类</h3>
<form action="class.php" method="post">
<?=makeselect($category,'who')?>移动到:<?=makeselect($category,'to')?>
<input type="hidden" id="op" name="op" value="move" />
<input type="submit" value="移动" />
</form>
<ul>
<?php
foreach ($category as $id){
echo '<li>'.$tree->getlayer($id, '|- ').$tree->getvalue($id).' <a href="class.php?op=del&cid='.$id.'">del</a> <a onclick="mod('.$id.')">edit</a> </li>';
}
?>
</ul>
完整的新闻无限级分类代码,可添加,删除,移动,修改


顶极节点 del   edit
|- 1级分类del   edit
|- 1级分类 del   edit
|- 1级分类 del   edit
|- |- 2级分类 del   edit
|- |- |- 3级分类 del  edit
|- |- |-|- 4级分类 del  edit
|- 1级分类 del   edit


<!--

create database `sortclass`default charset utf8;
create table if not exists `class` (
`cid` mediumint(8) unsigned not null auto_increment,
`pid` mediumint(8) unsigned not null,
`cname` varchar(50) not null,
primary key (`cid`),
key `pid` (`pid`)
) engine=myisam default charset=utf8;

--->

COOKIE 是在网页内容发送之前。先发送出去的信息。也就是说。在这之前不能有任何的 HTML 代码和 echo 等,即使是一个空格也是不允许的。。

在php教程中,要用 setcookie 函数来注册 cookie 变量,你上面的代码没有注册 cookie 变量。怎么可能送得过去呢。正确的写法如下:

*/
$status=0;                //定义状态
if(isset($_cookie['mytstcky'])&&($_cookie['mytstcky']=="chocchip")) $status=1; //如果已经设置变量
if(!isset($_get['cchk']))             //如果没有设置变量
{
  setcookie("mytstcky","chocchip");          //发送cookie信息
  header("location:$php_self?cchk=1");         //发送标头
  exit;                //结束php代码
}
?>
<html>
<head><title>cookie check</title></head>
<body bgcolor="#ffffff" text="#000000">
cookie check status:
<?php
printf('<font color="#%s">%s</font><br>;',$status ? "00ff00" : "ff0000",$status ? "passed!" : "failed!");               //根据条件输出不同内容
?>
</body>
</html>

mt_rand ( int $min , int $max )函数用于生成随机整数,其中$min–$max为ascii码的范围,这里取33-126,可以根据需要调整范围,如ascii码表中97–122位对应a–z的英文字母,具体可参考ascii码表;chr ( int $ascii )函数用于将对应整数$ascii转换成对应的字符。

  代码:

function create_password($pw_length = 8)
{
    $randpwd = '';
    for ($i = 0; $i < $pw_length; $i++)
    {
        $randpwd .= chr(mt_rand(33, 126));
    }
    return $randpwd;
}
// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);


  方法二:

  1、预置一个的字符串$chars,包括a–z、a–z、0–9以及一些特殊字符;

  2、在$chars字符串中随机取一个字符;

  3、重复第二步n次,可得长度为n的密码。

  代码:

function generate_password( $length = 8 ) {
    // 密码字符集,可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
    $password = '';
    for ( $i = 0; $i < $length; $i++ )
    {
        // 这里提供两种字符获取方式
        // 第一种是使用 substr 截取$chars中的任意一位字符;
        // 第二种是取字符数组 $chars 的任意元素
        // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }
    return $password;
}

下面利用preg_match举了两个实例一个是获取指定区域内容,一个是获取内容中的url地址。

$string="<b>example:</b><div align=left>this is a test</div>";   //定义字符串
$pattern="|<[^>]+>(.*)</[^>]+>|u";        //定义正则表达式模式
/*该匹配模式的意义是:以"<"开头后面跟1到多个不为">"的字符加上结尾内容;子模式中的".*"表示0到多个任意字符,再加上以"<"开头后面跟1到多个不为">"的字符加上">"符号。*/
preg_match_all($pattern,$string,$out,preg_pattern_order);  //进行preg_mathc_all处理
echo $out[0][0]; 
echo ",";
echo $out[0][1];
echo "<p>";
echo $out[1][0];
echo ",";
echo $out[1][1];

//实例二

//以下代码用于提取域名
$http="http://www.111cn.net";       //定义网址
preg_match("/^(http://)?([^/]+)/i",$http,$matches);     //进行正则表达式匹配
$host=$matches[2];           //把结果数组元素赋值到变量
preg_match("/[^./]+.[^./]+$/",$host,$matches);     //进行正则表达式匹配
echo "domain name is:{$matches[0]}n";       //输出结果域名

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • Element PageHeader页头的使用方法

    这篇文章主要介绍了Element PageHeader页头的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-27
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • PHP函数strip_tags的一个bug浅析

    PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
  • SQL Server中row_number函数的常见用法示例详解

    这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • PHP加密解密函数详解

    分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
  • php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法

    最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天原因,最后找到了问题所在。 1.使用 PEAR 的...2015-10-21
  • C#中加载dll并调用其函数的实现方法

    下面小编就为大家带来一篇C#中加载dll并调用其函数的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#虚函数用法实例分析

    这篇文章主要介绍了C#虚函数用法,实例分析了C#中虚函数的功能与基本使用技巧,需要的朋友可以参考下...2020-06-25