我想通过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以外,搜索框是否还有别的特征,可以通过正则提取出?谢谢。

解决方案 »

  1.   

    正则抓网站,看看这个东西:simple html dom
    http://simplehtmldom.sourceforge.net/manual.htm
      

  2.   

    不同的网站,正则肯定不一样,所以只能针对某一个来写正则,可根据页面的特征区分开不同的form
      

  3.   


    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">
      

  4.   

    当表单捕获到后在匹配"search",如果有最好,如果没有取第一表单更正一下前面的代码:if(preg_match_all("/<form[^>]*?>(.*?)<\/form>/is",$contents,$match)){
      print_r($match);
    }
      

  5.   

    正则表达式30分钟入门教程   求人不如考己http://deerchao.net/tutorials/regex/regex.htm
      

  6.   


    谢谢大家。life169,问个问题。如何在正则前加个判断,name 或 id 里面有search,再提取form栏?
      

  7.   

    preg_match("/<form[^>]*?(name\s+=\s+\"search\"|id\s+=\s+\"search\")[^>]*?>(.*?)<\/form>/is",$contents,$match)
      

  8.   

    感谢lijpwsw,得出结果为空,这样是不是只能正则出 name 或 id 里面为search,而不是name 或 id 包含文字 search? 如何能做到name 或 id 里包含文字 search?谢谢。
      

  9.   

    不太明白你的意思 preg_match("/<form[^>]*?(name\s*=\s*\"search\"|id\s*=\s*\"search\")[^>]*?>(.*?)<\/form>/is",$contents,$match);
      

  10.   

    比方说 id = mysite-search 或者 class = searchbox 等等,id或class不单只包含文字search
      

  11.   


    preg_match("/<form[^>]*?(name\s*=\s*[\"|']*search*[\"|']|id\s*=\s*[\"|']*search*[\"|'])[^>]*?>(.*?)<\/form>/is",$contents,$match)
    在采集时候加入判断有name="xxxsearchxxxx"或者name='xxxsearchxxx' 或者 id="xxxsearchxxx" 或者id='xxxsearchxxx' 就采集。(xxx表示任意字符)
      

  12.   

    @life169, 输出结果为空啊…… <?php
    $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);
    }
    ?>
      

  13.   

    $str = file_get_contents('http://ilivewithadisability.com/');
    if(preg_match("/<form(?:.*?)(?:name\s*=\s*[\"|'](?:.*?)search(?:.*?)[\"|']|id\s*=\s*[\"|'](?:.*?)search(?:.*?)[\"|'])[^>]*?>((\s|.)*?)<\/form>/is",$str,$match)){
      print_r($match);
    }
      

  14.   

    上面的有点问题
    $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);
    }
      

  15.   

    lijpwsw,谢谢你,最后那个代码可以了。