在文件中查找字符串并显示行号

时间:2014-08-19 10:08:22

标签: php lines

我是PHP的新手,所以我需要帮助来构建这个脚本。

我有一个包含以下行的file.txt文件:

aaaa 1234
bbba 1234
aaaa 1236
cccc 1234
aaaa 1238
dddd 1234

我想找到字符串" aaaa "并打印:

  

String" aaaa"在线上发现3次:1,3,5。

更好的是它可以打印这些行。

我试过这段代码:

<?
function find_line_number_by_string($filename, $search, $case_sensitive=false ) {
        $line_number = '';
        if ($file_handler = fopen($filename, "r")) {
           $i = 0;
           while ($line = fgets($file_handler)) {
              $i++;
              //case sensitive is false by default
                if($case_sensitive == false) {    
                $search = strtolower($search);  //convert file and search string
                $line = strtolower($line);         //to lowercase
              }
              //find the string and store it in an array
              if(strpos($line, $search) !== false){
                $line_number .=  $i.",";
              }
           }
           fclose($file_handler);
        }else{
            return "File not exists, Please check the file path or filename";
        }
        //if no match found
        if(count($line_number)){
            return substr($line_number, 0, -1);
        }else{
            return "No match found";
        }
    }


$output = find_line_number_by_string('file.txt', 'aaaa');

print "String(s) found in ".$output;

?>

但我不知道如何计算找到的字符串总数(3)并打印每个找到的行。

提前感谢。

3 个答案:

答案 0 :(得分:2)

有很多方法可以产生相同的最终结果但具体细节不同。

假设您的输入不够大而无法一次性将其加载到内存中,最方便的方法之一是使用file将文件的内容读入一行数组,然后preg_grep过滤数组并仅保留匹配的行。生成的数组键将是行号,值将是匹配的整行,完全符合您的要求。

示例:

$lines = file('file.txt');
$matches = preg_grep('/aaaa/', $lines);

echo count($matches)." matches found.\n";
foreach ($matches as $line => $contents) {
    echo "Line ".($line + 1).": ".$contents."\n";
}

答案 1 :(得分:-1)

$str = "aaaa";

$handle = fopen("your_file.txt", "r");
if ($handle) {

echo "String '".$str."' found at lines : ";

    $count = 0;
    $arr_lines = array();
    while (($line = fgets($handle)) !== false) {
        $count+=1;
        if (strpos($line, $str) !== false) {
           $arr_lines[] = $count;
         }
    }

    echo implode(", ", $arr_lines).".";
}

更新2:

$file = "your_file.txt";
$str = "aaaa;";

$arr = count_line_no($file, $str);

if(count($arr)>0)
{
     echo "String '".$str."' found at lines : ".implode(", ", $arr).".";;
}
else
{
     echo "String '".$str."' not found in file ";
}

function count_line_no($file, $str)
{
    $arr_lines = array();
    $handle = fopen("your_file.txt", "r");
    if ($handle) {
        $count = 0;
        $arr_lines = array();
        while (($line = fgets($handle)) !== false) {
            $count+=1;
            if (strpos($line, $str) !== false) {
               $arr_lines[] = $count;
             }
        }
    }

    return $arr_lines;
}

答案 2 :(得分:-1)

**尝试解决你的问卷**

if(file_exists("file.txt")) // check  file is exists
{
    $f = fopen("file.txt", "r");

        // Read line by line until end of file

    $row_count = 0;
    while(!feof($f))
    { 
        $row_count += 1;
        $row_data = fgets($f);
        $findme   = 'aaaa';
        $pos = strpos($row_data, $findme);

        if ($pos !== false)
        {
            echo "The string '$findme' was found in the string '$row_data'";
            echo "<br> and line number is".$row_data;
        }
        else
        {
             echo "The string '$findme' was not found ";
        }

    }

    fclose($f);

}