Read file - Search criterion + Multiple Outputs & Results

Hi all,
I have a question regarding a search in a local file for lines with one search criterion.
Currently the process stops once the line with the search criterion is found the first time.
I want it working with one search criterion finding multiple lines till the end of the file and giving all those lines back.

The local file: (test.txt)(Sample) Richard says Hello Lisa says Hi Peter says Good Morning Richard says Bye Sarah says Hello

[php]<?php
$search = “Richard”;
$tmp = ‘’;
$buffer = ‘’;
$fileaddress = ‘./test.txt’;
$file_handle = fopen($fileaddress, ‘r’);
for($i=1; !feof($file_handle); $i++){
$tmp = $buffer;
$buffer = fgets($file_handle, 16384);
if(strpos($tmp . $buffer, $search) !== false){
echo $buffer;
break;
}
}
fclose($file_handle);
?>[/php]

Thank you for your help.

Peter

Just remove the break

Hi all,
I have already tried this, but it is not the solution.
If I remove the break, it is giving all others back until the end of the list (also those not matching the search criterion), beginning with the line matching the first search criterion.

It was because of your tmp = buffer stuff. When you append the strings together and then search through it then they will contain the search word from the first line.

[php]<?php

$search = “Richard”;
$tmp = ‘’;
$buffer = ‘’;
$fileaddress = ‘./test.txt’;
$file_handle = fopen($fileaddress, ‘r’);
for($i=1; !feof($file_handle); $i++){
$buffer = fgets($file_handle, 16384);
if(strpos($buffer, $search) !== false){
echo $buffer . ‘
’;
}
}
fclose($file_handle);[/php]

Hi JimL,
works perfect.

Thank you

Sponsor our Newsletter | Privacy Policy | Terms of Service