你这个函数应该传入一个字符串才对
假设有一个文件内容与你的相同,名字为"test"function searchInLine( $file, $wantStr )
{
 $arr = explode( "\r\n", file_get_contents($file);
foreach( $arr as $k => $line )
{
    if( str_pos( $line, $wantStr ) !== false )
            return $k;
}
}$wantStr = "kylix";
$file    = "test";
echo searchInLine( $file, $wantStr );

解决方案 »

  1.   

    上面的有错误,修改了一下,完整代码如下<?php$str = <<<STR
    1 #just test
    2 user-name="kylix",password="111"
    3 #found the result
    STR;function searchInLine( $str, $needle )
    {
    $arr = explode( "\r\n", $str );
    foreach( $arr as $k => $line )
    {
    if( strpos( $line, $needle ) !== false )
    return $k+1;
    }
    }$wantStr = "kylix";
    echo searchInLine( $str, $wantStr );
    ?>
      

  2.   

    楼上的..好像不行.
    我的filearray 已经是文件数组了.也就是说所有行的内容已经被读到filearray中了.
      

  3.   

    而且还是:
    Invalid argument supplied for foreach() 
    是不是版本问题啊???
      

  4.   

    <?php
    function searchInLine($haystack, $needle) {
      list($s) = explode($needle, $haystack);
      return substr_count($s, "\n") + 1;
    }$str = <<< TEXT
    1   #just test
    2   user-name="kylix",password="111"
    3   #found the result
    TEXT;$ch = 'kylix';echo searchInLine($str, $ch);
    ?>
      

  5.   

    你的函数的问题在于 $filearray 未定义!!!
      

  6.   

    肯定有定义的.并且已经把每一行的数据给了filearray的每一个元素
      

  7.   

    function searchInLine($sStr="") {
                    $tempArray = array();
                    foreach($filearray as $lineNo => $lineValue) { //<===只在这里出现唯一的$filearray
                            if(eregi($sStr,$lineValue)) {
                                    $tempArray[$lineNo] = $lineValue;
                                    $found = 1;
                            }
                    }
                    if($found==1) {
                            return $tempArray;
                    } else {
                            return false;
                    }
            }
    啃你的腚去吧!
      

  8.   

    先explode,再查询字符串在该行在不在。到底是explode \n还是\r\n是和OS有关的。楼主可以分别试下。