php正则获取关键字、标题、网页描述代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-07-24 14:47:54 浏览: 评论:0
本文章来给大家介绍关于php正则获取关键字、标题、网页描述代码,有需要了解的同学可进入参考.
获取关键字,代码如下:
- function get_keywords($html)
- {
- $html=strtolower($html);
- preg_match("@<head[^>]*>(.*?)</head>@si",$html, $regs);
- $headdata = $regs[1];
- preg_match("/<meta +name *=["']?keywords["']? *content=["']?([^<>"]+)["']?/i", $headdata, $res);
- if (isset ($res)) {
- $keywords = $res[1];
- }
- if ($keywords == "") $keywords = "无";
- $keywords=replace_word(textcut($keywords,250));
- $keywords=str_replace("-",",",$keywords);
- $keywords=str_replace(",",",",$keywords);
- $keywords=str_replace(" ",",",$keywords);
- $keywords=str_replace("|",",",$keywords);
- $keywords=str_replace("、",",",$keywords);
- $keywords=str_replace(",,",",",$keywords);
- $keywords=str_replace("<","",$keywords);
- $keywords=str_replace(">","",$keywords);
- return addslashes(trim($keywords));
- }
获取标题,代码如下:
- function get_title($html)
- {
- $html=strtolower($html);
- $title = str_replace(" - ",",",cut($html,"<title>", "</title>" ));
- //if ($title == "") $title = "无标题";
- if ($title) $title=replace_word(textcut($title,80));
- if ($title) $title=preg_replace("/<(.*?)>/","",$title);
- return addslashes(trim($title));
- }
获取网页描述description,代码如下:
- function get_description($html)
- {
- $html=strtolower($html);
- preg_match("@<head[^>]*>(.*?)</head>@si",$html, $regs);
- $headdata = $regs[1];
- preg_match("/<meta +name *=["']?description["']? *content=["']?([^<>"]+)["']?/i", $headdata, $res);
- if (isset ($res)) {
- $description = $res[1];
- }
- if ($description == "") $description = "无";
- $description=replace_word(textcut($description,250));
- $description=str_replace("-",",",$description);
- $description=str_replace(",",",",$description);
- $description=str_replace(" ",",",$description);
- $description=str_replace("|",",",$description);
- $description=str_replace("、",",",$description);
- $description=str_replace(",,",",",$description);
- $description=str_replace("<","",$description);
- $description=str_replace(">","",$description);
- return addslashes(trim($description));
- }
Tags: php正则 关键字 网页描述
- 上一篇:php中邮箱email 电话等格式的验证
- 下一篇:php匹配网址的正则表达式
相关文章
- ·php正则连接中文本程序(2013-11-12)
- ·三分钟学会PHP正则表达式(2013-11-13)
- ·巧用PHP正则表达式判断IP地址(2013-11-13)
- ·php用户注册时常用的一些检验函数总结(2014-01-05)
- ·php中ip地址 email格式 电话号码正则验证(2014-01-06)
- ·php正则字符串中图片地址表达式(2014-01-15)
- ·php 汉字正则表达式实例详解(2014-07-07)
- ·php 获取网页内的图片地址正则表达式(2014-07-18)
- ·php正则表达式替换URL链接地址为指定url(2014-07-26)
- ·php中网址、email、手机号码正则表达代码(2014-07-29)
- ·php中常用的正则表达式规则总结(2014-07-30)
- ·php中固定电话号码和手机号码正则表达式验证(2014-07-30)
- ·php利用正则过滤链接、标签,空格,换行符程序(2014-07-30)
- ·php 手机号码正则表达试程序代码(2014-07-31)
- ·php正则表达式之正则字符中的图片地址(2014-08-01)
- ·php正则之删除或转换字符串中url地址(2014-08-02)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)