Home > Backend Development > PHP Tutorial > 正则获取网页源码keyword和description ,蛋有点疼

正则获取网页源码keyword和description ,蛋有点疼

PHP中文网
Release: 2016-06-23 14:00:45
Original
1512 people have browsed it

情况一:  

<META NAME="description" CONTENT="华尔街债券(bond.wswire.com)
是全球第一债券网站,为您提供全球债券市场最迅速最专业的债券资讯和全天候的债券理财、债券评级及报价服务,
华尔街债券覆盖交易所债券市场、银行间债券市场、银行同业拆借及公开市场等各方面的债券信息服务。华尔街债券汇聚多家顶级专业机构分析研究报告、每日两次的精确数据分析以及图文并茂的市况报道。"><META NAME="keywords" CONTENT="华尔街,电讯,华尔街电讯,全球债券,国债,债券,债市,企业债,企债,可转债,回购,正回购,赎回,债券公告,利率,金融债,央行,短期融资券,记账式国债,货币政策,财经,汇率,票据,公开市场,稳定收益,公债,柜台交易,银行间债市,同业拆借,债券资讯,融资债,债券理财,债券评级,银行间市场,交易所市场,海外市场,央行票据">
Copy after login


情况二:

<meta name=keywords content="微波炉使用高火档能耗低更节能(图),环保新知,,,微波炉,,,高火,,,节能,,,省电,,"><meta name=description content="微波炉使用高火档能耗低更节能(图)">
Copy after login



注意:可能大小写,还有就是name,和content属性[color=#FF6600] 位置 不一样[/color]

小弟试着写了一下,只能匹配一写网页,不知道有什么问题。大牛请解答,拜谢!
keyword:

 1.preg_match("/<meta[\s]+name=[&#39;\"]keywords[&#39;\"] content=[&#39;\"](.*)[&#39;\"]/isU",$this->tmpHtml,$inarr);
 2.preg_match("/<meta[\s] content=[&#39;\"](.*)[&#39;\"] name=[&#39;\"]keywords[&#39;\"]/isU",$this->tmpHtml,$inarr2);
Copy after login
1.preg_match("/<meta[\s]+name=[&#39;\"]description[&#39;\"] content=[&#39;\"](.*)[&#39;\"]/isU",$this->tmpHtml,$inarr);
2.preg_match("/<meta[\s]+content=[&#39;\"](.*)[&#39;\"] name=[&#39;\"]description[&#39;\"]/isU",$this->tmpHtml,$inarr2);
Copy after login

说明:一些网页能匹配,一些不能


回复讨论(解决方案)

哦对了,忘了说明了,有的网页 是这样的:




keywords 和description没有双引号。匹配不了、希望大哥们帮我完善一下,最好测试通过

不是有个get_meta_tags函数么

不是有个get_meta_tags函数么
+1,可以返回一个meta的数组的,再提取需要的就是了

呵呵,见笑了,恩谢谢啊,foolbirdflyfirst yangball

name在前面:

<meta(\s)name=(\&#39;|\"|)keywords(\&#39;|\"|)(\s*)content=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)><meta(\s)name=(\&#39;|\"|)
keywords(\&#39;|\"|)(\s*)content=(\&#39;|\"|)|(\&#39;|\"|)(\s*)><meta(\s)name=(\&#39;|\"|)description(\&#39;|\"|)(\s*)
content=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)><meta(\s)name=(\&#39;|\"|)description(\&#39;|\"|)(\s*)content=(\&#39;|\"|)|(\&#39;|\"|)(\s*)>
Copy after login


name在后面:

<meta(\s)content=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)name=(\&#39;|\"|)keywords(\&#39;|\"|)(\s*)> 
<meta(\s)content=(\&#39;|\"|)|(\&#39;|\"|)(\s*)name=(\&#39;|\"|)keywords(\&#39;|\"|)(\s*)> 
<meta(\s)content=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)name=(\&#39;|\"|)description(\&#39;|\"|)(\s*)> 
<meta(\s)content=(\&#39;|\"|)|(\&#39;|\"|)(\s*)name=(\&#39;|\"|)description(\&#39;|\"|)(\s*)>
Copy after login

name在前面:

<meta(\s)name=(\&#39;|\"|)keywords(\&#39;|\"|)(\s*)content=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)><meta(\s)name=(\&#39;|\"|)
keywords(\&#39;|\"|)(\s*)content=(\&#39;|\"|)|(\&#39;|\"|)(\s*)><meta(\s)name=(\&#39;|\"|)description(\&#39;|\"|)(\s*)
content=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)><meta(\s)name=(\&#39;|\"|)description(\&#39;|\"|)(\s*)content=(\&#39;|\"|)|(\&#39;|\"|)(\s*)>
Copy after login

name在后面:

<meta(\s)content=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)name=(\&#39;|\"|)keywords(\&#39;|\"|)(\s*)><meta(\s)
content=(\&#39;|\"|)|(\&#39;|\"|)(\s*)name=(\&#39;|\"|)keywords(\&#39;|\"|)(\s*)><meta(\s)content=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)
name=(\&#39;|\"|)description(\&#39;|\"|)(\s*)><meta(\s)content=(\&#39;|\"|)|(\&#39;|\"|)(\s*)name=(\&#39;|\"|)
description(\&#39;|\"|)(\s*)>
Copy after login

根据楼上,进一步得出:
name在前:

<(\s*)(meta|META|Meta)(\s*)(name|NAME|Name)=(\&#39;|\"|)(keywords|KEYWORDS|Keywords)(\&#39;|\"|)(\s*)
(content|CONTENT|Content)=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)><(\s*)(meta|META|Meta)(\s*)(name|NAME|Name)=(\&#39;|\"|)(
keywords|KEYWORDS|Keywords)(\&#39;|\"|)(\s*)(content|CONTENT|Content)=(\&#39;|\"|)|(\&#39;|\"|)(\s*)><(\s*)(meta|META|Meta)
(\s*)(name|NAME|Name)=(\&#39;|\"|)(description|DESCRIPTION|Description)(\&#39;|\"|)(\s*)
(content|CONTENT|Content)=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)><(\s*)(meta|META|Meta)(\s*)(name|NAME|Name)=(\&#39;|\"|)
(description|DESCRIPTION|Description)(\&#39;|\"|)(\s*)(content|CONTENT|Content)=(\&#39;|\"|)|(\&#39;|\"|)(\s*)>
Copy after login

name在后:

<(\s*)(meta|META|Meta)(\s*)(content|CONTENT|Content)=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)(name|NAME|Name)=(\&#39;|\"|)(keywords|KEYWORDS|Keywords)(\&#39;|\"|)(\s*)><(\s*)(meta|META|Meta)(\s*)(content|CONTENT|Content)=(\&#39;|\"|)|(\&#39;|\"|)(\s*)(name|NAME|Name)=(\&#39;|\"|)(keywords|KEYWORDS|Keywords)(\&#39;|\"|)(\s*)><(\s*)(meta|META|Meta)(\s*)(content|CONTENT|Content)=(\&#39;|\"|)(.*)(\&#39;|\"|)(\s*)(name|NAME|Name)=(\&#39;|\"|)(description|DESCRIPTION|Description)(\&#39;|\"|)(\s*)><(\s*)(meta|META|Meta)(\s*)(content|CONTENT|Content)=(\&#39;|\"|)|(\&#39;|\"|)(\s*)(name|NAME|Name)=(\&#39;|\"|)(description|DESCRIPTION|Description)(\&#39;|\"|)(\s*)>
Copy after login

 以上就是正则获取网页源码keyword和description ,蛋有点疼的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template