function getKeyWord($url,$KeyWord)
{
$t_a=$KeyWord;
$pm=array();
foreach((array)$t_a as $k=>$v)
{
for ($i=0; $i <10; $i++)
{
      
      $link= "http://www.baidu.com/s?lm=0&si=&rn=10&ie=utf-8&ct=0&wd=$KeyWord&pn={$i}0&ver=0&cl=3";    
      $html = file_get_contents($link);           
      preg_match_all('/<td class=f><a onclick=".*?" href="(.*?)" target="_blank">/', $html, $matches);      //print_r($matches[1]);
      //echo $url;
             for($j=1; $j<100;$j++)
       {
        if ($matches[$j]=$url)
        {
        $pm[$k]=$j+1;
        print_r($pm[$k]);
       
        break;
        }
       
       
       }
}
}
}
这个根据传进来的URL  遍历数组,然后提取这个URL是第几位。  大家看看我这个代码,哪里错了 最后输出 得不到啊
小弟菜鸟,各位大哥帮忙

解决方案 »

  1.   

    function getKeyWord($url,$KeyWord)
    {
        for ($i=0; $i <10; $i++)
           {
          
          $link= "http://www.baidu.com/s?lm=0&si=&rn=10&ie=utf-8&ct=0&wd=$KeyWord&pn={$i}0&ver=0&cl=3";    
          $html = file_get_contents($link);           
          preg_match_all('/<td class=f><a onclick=".*?" href="(.*?)" target="_blank">/', $html, $matches);
      
         print_r($matches[1]);
         // echo $url;
          foreach ($matches as $value)    
         {
       
          for($j=1; $j<100;$j++)
           {
            $pos = stripos($matches[1][$j], $url);
            if ($pos!==false)
            {
            $pm[$value]=$j+1;
            print_r($pm[$value]);       
            break;
           
            }
           
           
           }
         }  
    }
    }    
    ?>
    改不下去了,能力到此,还请大家帮忙。
      

  2.   

    1.
    if ($matches[$j]=$url) 应为
    if ($matches[$j]==$url)2.
    根据你的正则表达式, $matches只能是二维数组.$j不能大于1