我想通过PHP正则,获取某几个网站的搜索框。但是我正则水平有限,试写了好几次都没有成功。下面是我的代码。<?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以外,搜索框是否还有别的特征,可以通过正则提取出?谢谢。
$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以外,搜索框是否还有别的特征,可以通过正则提取出?谢谢。
http://simplehtmldom.sourceforge.net/manual.htm
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">
print_r($match);
}
谢谢大家。life169,问个问题。如何在正则前加个判断,name 或 id 里面有search,再提取form栏?
preg_match("/<form[^>]*?(name\s*=\s*[\"|']*search*[\"|']|id\s*=\s*[\"|']*search*[\"|'])[^>]*?>(.*?)<\/form>/is",$contents,$match)
在采集时候加入判断有name="xxxsearchxxxx"或者name='xxxsearchxxx' 或者 id="xxxsearchxxx" 或者id='xxxsearchxxx' 就采集。(xxx表示任意字符)
$str = file_get_contents('http://ilivewithadisability.com/');
if(preg_match("/<form[^>]*?(name\s*=\s*[\"|']*search*[\"|']|id\s*=\s*[\"|']*search*[\"|'])[^>]*?>(.*?)<\/form>/is",$str,$match)){
print_r($match);
}
?>
if(preg_match("/<form(?:.*?)(?:name\s*=\s*[\"|'](?:.*?)search(?:.*?)[\"|']|id\s*=\s*[\"|'](?:.*?)search(?:.*?)[\"|'])[^>]*?>((\s|.)*?)<\/form>/is",$str,$match)){
print_r($match);
}
$str = file_get_contents('http://ilivewithadisability.com/');
if(preg_match("/<form(?:.*?)(?:name\s*=\s*[\"|'](?:.*?)search(?:.*?)[\"|']|id\s*=\s*[\"|'](?:.*?)search(?:.*?)[\"|'])[^>]*?>(.*?)<\/form>/is",$str,$match)){
print_r($match);
}