rgb192
asked on
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
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/>";
?>
Please give an example of what the output should be.
Is this what you are wanting?
Output:
text occurs at Line # 1 Offset 6
text occurs at Line # 3 Offset 11
<?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 />";
}
}
?>
Output:
text occurs at Line # 1 Offset 6
text occurs at Line # 3 Offset 11
ASKER
change the input from $string
to be /folder1
and to read all the files in /folder1
to be /folder1
and to read all the files in /folder1
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);
}
?>
ASKER
if (
preg_match_all('#([^\n]*?t
){
Array occurs at Line # 3 Offset
@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.
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.
ASKER
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,,,,
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
30/03/2011,702002,35580138
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.
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.
ASKER
I don't understand how to change code
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
works great, thanks
you should explode your search string by lines, and preg_match each line