首页 > 语言 > PHP > 正文

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

2024-09-04 11:47:24
字体:
来源:转载
供稿:网友

本文章来给大家介绍关于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)); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表