你这个函数应该传入一个字符串才对
假设有一个文件内容与你的相同,名字为"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 );
假设有一个文件内容与你的相同,名字为"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 #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 );
?>
我的filearray 已经是文件数组了.也就是说所有行的内容已经被读到filearray中了.
Invalid argument supplied for foreach()
是不是版本问题啊???
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);
?>
$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;
}
}
啃你的腚去吧!