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

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

获取关键字,代码如下:

  1. function get_keywords($html)
  2. {
  3. $html=strtolower($html);
  4. preg_match("@<head[^>]*>(.*?)</head>@si",$html, $regs);
  5. $headdata = $regs[1];
  6. preg_match("/<meta +name *=["']?keywords["']? *content=["']?([^<>"]+)["']?/i", $headdata, $res);
  7. if (isset ($res)) {
  8. $keywords = $res[1];
  9. }
  10. if ($keywords == "") $keywords = "无";
  11. $keywords=replace_word(textcut($keywords,250));
  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. $keywords=str_replace("<","",$keywords);
  19. $keywords=str_replace(">","",$keywords);
  20. return addslashes(trim($keywords));
  21. }

获取标题,代码如下:

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

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

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