当前位置:首页 > PHP教程 > 正则表达式 > 列表

php正则获取关键字、标题、网页描述代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-07-24 14:47:54 浏览: 评论:0 

本文章来给大家介绍关于php正则获取关键字、标题、网页描述代码,有需要了解的同学可进入参考.

获取关键字,代码如下:

  1. function get_keywords($html
  2. $html=strtolower($html); 
  3. preg_match("@<head[^>]*>(.*?)</head>@si",$html$regs);  
  4. $headdata = $regs[1]; 
  5. preg_match("/<meta +name *=["']?keywords["']? *content=["']?([^<>"]+)["']?/i", $headdata$res); 
  6. if (isset ($res)) { 
  7. $keywords = $res[1]; 
  8. if ($keywords == ""$keywords = "无"
  9. $keywords=replace_word(textcut($keywords,250)); 
  10. $keywords=str_replace("-",",",$keywords); 
  11. $keywords=str_replace(",",",",$keywords); 
  12. $keywords=str_replace(" ",",",$keywords); 
  13. $keywords=str_replace("|",",",$keywords); 
  14. $keywords=str_replace("、",",",$keywords); 
  15. $keywords=str_replace(",,",",",$keywords); 
  16. $keywords=str_replace("<","",$keywords); 
  17. $keywords=str_replace(">","",$keywords); 
  18. return addslashes(trim($keywords)); 

获取标题,代码如下:

  1. function get_title($html
  2.   $html=strtolower($html); 
  3.  $title = str_replace(" - ",",",cut($html,"<title>""</title>" )); 
  4. //if ($title == "") $title = "无标题"; 
  5. if ($title$title=replace_word(textcut($title,80)); 
  6. if ($title$title=preg_replace("/<(.*?)>/","",$title); 
  7. return addslashes(trim($title)); 

获取网页描述description,代码如下:

  1. function get_description($html
  2. $html=strtolower($html); 
  3. preg_match("@<head[^>]*>(.*?)</head>@si",$html$regs);  
  4. $headdata = $regs[1]; 
  5. preg_match("/<meta +name *=["']?description["']? *content=["']?([^<>"]+)["']?/i", $headdata$res); 
  6.       if (isset ($res)) { 
  7.        $description = $res[1]; 
  8.       } 
  9. if ($description == ""$description = "无"
  10. $description=replace_word(textcut($description,250)); 
  11. $description=str_replace("-",",",$description); 
  12. $description=str_replace(",",",",$description); 
  13. $description=str_replace(" ",",",$description); 
  14. $description=str_replace("|",",",$description); 
  15. $description=str_replace("、",",",$description); 
  16. $description=str_replace(",,",",",$description); 
  17. $description=str_replace("<","",$description); 
  18. $description=str_replace(">","",$description); 
  19. return addslashes(trim($description)); 

Tags: php正则 关键字 网页描述

分享到: