• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

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

0
rgb192
Asked:
rgb192
1 Solution
 
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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
 
rgb192Author Commented:
works great, thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now