正则获取网站的搜索框
我想通过PHP正则,获取某几个网站的搜索框。但是我正则水平有限,试写了好几次都没有成功。下面是我的代码。
PHP code
另外,搜索框一般都以FORM作为HTML标签,而网站的注册登陆系统,甚至评论也用的是FORM,依靠大家的经验,有没有办法区分?
或者除了FORM以外,搜索框是否还有别的特征,可以通过正则提取出?谢谢。
PHP code
<?php $lines = file('http://ilivewithadisability.com/'); foreach ($lines as $line_num => $line) { $source = htmlspecialchars($line) . "<br />\n"; preg_match_all('~<form[^>]*>(.+)(?!</form)</form>~', $source, $match); var_dump($match); echo $source; } ?>
另外,搜索框一般都以FORM作为HTML标签,而网站的注册登陆系统,甚至评论也用的是FORM,依靠大家的经验,有没有办法区分?
或者除了FORM以外,搜索框是否还有别的特征,可以通过正则提取出?谢谢。
作者: changjay 发布时间: 2011-01-07
每个站点的都不一定相同的,你说你所要的特征吧。
作者: yangball 发布时间: 2011-01-07
正则抓网站,看看这个东西:simple html dom
http://simplehtmldom.sourceforge.net/manual.htm
http://simplehtmldom.sourceforge.net/manual.htm
作者: baiyuxiong 发布时间: 2011-01-07
不同的网站,正则肯定不一样,所以只能针对某一个来写正则,可根据页面的特征区分开不同的form
作者: lijpwsw 发布时间: 2011-01-07
PHP code
另外,一般的搜索表单的name 或 id 里面有search
例如:http://ilivewithadisability.com/的form就是
<form action="http://ilivewithadisability.com/search" method="post" id="search-form">
if(preg_match("/<form[^>]*?>(.*?)<\/form>/is",$contents,$match)){ print_r($match); }
另外,一般的搜索表单的name 或 id 里面有search
例如:http://ilivewithadisability.com/的form就是
<form action="http://ilivewithadisability.com/search" method="post" id="search-form">
作者: life169 发布时间: 2011-01-07
当表单捕获到后在匹配"search",如果有最好,如果没有取第一表单
更正一下前面的代码:
PHP code
更正一下前面的代码:
PHP code
if(preg_match_all("/<form[^>]*?>(.*?)<\/form>/is",$contents,$match)){ print_r($match); }
作者: life169 发布时间: 2011-01-07
之所以取第一表单,是因为,一般搜索框都在上面。
作者: life169 发布时间: 2011-01-07