return every line where there is a search term

want code to return every line where there is a search term

in a folder

the current code finds search term in a $string
<?php
$string="
term1 text
this line does not match
Text term2 text
and this line is a dud too
";
preg_match_all('#([^\n]*?term[^\n]*?)\n#s',$string,$matches,PREG_OFFSET_CAPTURE);

echo "<pre>";
print_r ($matches[1]);
echo "</pre>";

foreach( $matches[1] as $aMatch )
    echo "<br/>{$aMatch[0]} occurs at offset {$aMatch[1]} <br/>";

?>

Open in new window

LVL 1
rgb192Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TiberiuGalCommented:
Hi,
you should explode your search string by lines, and preg_match each line
0
ray-solomonCommented:
Please give an example of what the output should be.
0
ray-solomonCommented:
Is this what you are wanting?

<?php
$string="
term1 text
this line does not match
Text term2 text
and this line is a dud too
";

$lines = explode("\n", $string);

foreach ($lines as $line_num => $line) {
	if (preg_match('/term\d\s(\w+)/',$line,$matches)) {
		echo "<br /><b>{$matches[1]}</b> occurs at Line # {$line_num} Offset ".strpos($line,$matches[1])."<br />";
	}
    
}
?>

Open in new window



Output:

text occurs at Line # 1 Offset 6

text occurs at Line # 3 Offset 11
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

rgb192Author Commented:
change the  input from $string

 to be /folder1
and to read all the files in /folder1


0
ray-solomonCommented:
done.


<?php
$dir = 'folder1';
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $lines = file($dir.'/'.$file);
			foreach ($lines as $line_num => $line) {
				if (preg_match('/term\d\s(\w+)/',$line,$matches)) {
					echo "<br /><b>{$matches[1]}</b> occurs at Line # {$line_num} Offset ".strpos($line,$matches[1])."<br />";
				}
			}
        }
    }
    closedir($handle);
}
?>

Open in new window

0
rgb192Author Commented:



if (
preg_match_all('#([^\n]*?term[^\n]*?)\n#s',$line,$matches,PREG_OFFSET_CAPTURE);
){

Array occurs at Line # 3 Offset

0
ray-solomonCommented:
@rgb192
What was the point of posting that much data?
Please never do that again.


It appears your problem still has not been resolved.
The working solution I provided earlier was based entirely from the example data you provided.
If it did not work on your other data, then there must be something else you have not explained.


You should attach some data so we can help you better.
0
rgb192Author Commented:
sorry about posting that I copy and paste and did not think



this is in one txt file
there are many text files in that folder

so I search for '70'
I should get both lines as result


30/03/2011,701988,35580138,GBT4GR,1,12,,,,
30/03/2011,702002,35580138,GBT4GR,1,12,,,,
0
Ray PaseurCommented:
Instead of using \n you might want to use the context-aware constant PHP_EOL.

The general strategy seems to be fairly well handled here at ID:35263171.  You read the directory to get the file names, and you read the files with file() to get an array of lines from the file.  You iterate over the lines and look for the search string.  You do not need any regular expression if all you are looking for is a string.  You can use strpos() instead.  Faster and easier to code correctly.
0
rgb192Author Commented:
I don't understand how to change code
0
Shinesh PremrajanEngineering ManagerCommented:
Hope this helps
<?php
$string="
term1 text
this line does not match
Text term2 text
and this line is a dud too";

$dir = '.';
$searchfor="term2";

if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
			//&& is_dir($file)
            $lines = file($dir.'/'.$file);
			echo "<br/> COUNT:".count($lines)."File name :".$dir.'/'.$file;
			foreach ($lines as $line_num => $line) {
					if (strpos($line, $searchfor)) {					
						echo "<br />Line No. $line_num: $line<br />";
				}
			}
        }
    }
    closedir($handle);
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
works great, thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.