3

I’m trying to make a small php script to automate some work processes. What the script has to do is read a file (approximately 10-20kb per file). Then I need to search the file for some specific phrases and output – if the phrases occur – the linenumber(s) and the phrase.

For example, I’ve a text-file I’m reading and searching within. I search for the phrases “The flower is yellow”, “The horse is white”, and “mixed;”

Then my output is:

Line 4: “The horse is white” Line 19: “Mixed;” Line 22: “Mixed;” Line 99: “The flower is yellow” … and so on.

I’m using this code, which correctly output each line and the line number but I just can’t make a search routine that only output the searched phrases:

<?php
$lines = file('my-text-file.txt');

foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
?>  

My though was to use strpos and then every time I find an occurrence of the phrase it put it into an array (or dictionary) which the line number as key and the phrase as value but haven’t been able to make it work and I thought that there might be a better and more effective method.

Any help or suggestions in which direction I’ve to go would be very much appreciated.

Thanks
- Mestika

Emil Devantie Brockdorff
  • 4,724
  • 12
  • 59
  • 76

3 Answers3

2
function search ( $what = array (), $filePath ) {
    $lines = file ( $filePath );

    foreach ( $lines as $num => $line ) {
        foreach ( $what as $needle ) {
            $pos = strripos ( $line, $needle );
            if ( $pos !== false ) {
                echo "Line #<b>{$num}</b> : " . htmlspecialchars($line) . "<br />\n";
            }
        }
    }
}

#   Usage:
$what = array ( 'The horse is white', 'The flower is yellow', 'mixed' );
search ( $what );
Romeo M.
  • 3,218
  • 8
  • 35
  • 40
  • Hi Romeo, that worked pretty well - it returned the lines it should but with one exception. The last word "mixed", it searched for "mixed" no matter where it is. e.g. if there is a line with: "mixedcolor" then it returns it as well. Is there anyway to "trim" that so it only returns the whole word or if there is a symbol, like "." or ";" behind it? So "mixed," will be returned, but not "mixedcolor" ?? – Emil Devantie Brockdorff Jun 15 '11 at 12:50
  • i'm afraid u will need regex. It's what I wanted to post in the first place but thought people will start complaining it's slow but it's the most powerful and complex way of text matching. Instead of stripos you might use `preg_match('/\b' . preg_quote ( $needle, '/' ) . '\b/i', $line)`. Here's a read for word boundaries in regex and lots more about regex in general: http://www.regular-expressions.info/wordboundaries.html – Romeo M. Jun 15 '11 at 17:42
1

Create a script that will grep your input and pipe the results into your PHP script.

MuffinMan
  • 889
  • 1
  • 8
  • 28
0

Vide this: Find a string in a text file and add something to that line

Community
  • 1
  • 1
The Mask
  • 17,007
  • 37
  • 111
  • 185