tags:

views:

78

answers:

3

hello, I want to match a whole line matching a word/phrase on that line! I tried this:

preg_match("/PHRASE/i", $dictionary,$matches);

But I get only the matched word! but I need that whole line! And there more than 15K lines! So I am looking for best way to do it!

thanks in advance!

+3  A: 

Just use

^.*PHRASE.*$

^ to match the start of the line

.* to match any number of characters except newlines

PHRASE to match your keyword/phrase

.* as above

$ to match the end of the line.

You might also want to surround your PHRASE with word boundary anchors (if your phrase is indeed a word or words): ^.*\bPHRASE\b.*$ will match only if PHRASE is on its own (and not part of another word like PHRASEBOOK).

So, if you are applying your regex to every single line separately, use

preg_match('/^.*PHRASE.*$/i', $dictionary, $matches)

If you have all your lines inside a long multiline string and want to iterate over all the lines that contain your phrase, use:

preg_match_all('/^.*PHRASE.*$/im', $dictionary, $matches, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($matches[0]); $i++) {
    # Matched text = $matches[0][$i];
}

Note the /m modifier to allow ^ and $ to match at the start and end of each line (instead of just start/end of the string).

Tim Pietzcker
It worked after I added /m modifier! thanks! however, I surprised seeing the comparison of two solution given by you (Tim Pietzcker) and RobertPitt! Secon one (regex) takes about: 0.022 Seconds including file_get_contents(). but the first one (stripos) including file() takes about 0.0141 Seconds! Isn't it surprising? I believe regex will be much faster!
HungryCoder
btw, there is a study() function in PERL to optimize the string before matching, specially where repeated matchings are available! is there anything such for PHP?
HungryCoder
@HungryCoder, regex is usually slower than a couple of "normal" string operations like `strpos(...)`. Note however that such 0.022 vs. 0.0141 are no reliable numbers. For a a more reliable comparison, you'd have to perform the test at least a couple of thousand times and then take the average of it.
Bart Kiers
Are you including file_get_contents() within the speed tracking of the regex?
RobertPitt
yes, i am including file_get_contents inside the time tracking!
HungryCoder
not average, I just executed both stuff inside for() loop for 1000 times! regex taken 15.7409 Seconds and stripos 15.248 Seconds.
HungryCoder
however, stripos is taking much more time than strpos! about 10 seconds more! so now regex is taking 15+ seconds and stripos is taking 24+ seconds. but I don't see significant time change if I keep or remove the /i modifier from regex! so, I guess I can go with regex than stripos!
HungryCoder
+3  A: 

Use something like:

$handle = @fopen("file.txt", "r");
if ($handle)
{
    $matches = array();
    while (!feof($handle))
    {
        $buffer = fgets($handle);
        if(stripos($buffer,"mystring") !== false)
        {
            $matches[] = $buffer;
        }
    }
    fclose($handle);
}

Then you can see the results in $matches

RobertPitt
thanks sir for your answer! it worked! please check my comparison below!
HungryCoder
You should use `stripos` instead as the original regular expression has the *i* modifier.
Gumbo
oh yes, I forgot about it, thanks! the comparison is significant after I did this!
HungryCoder
Thanks, I forgot about case-sensitivity :) +1
RobertPitt
+1  A: 

try

preg_grep('/whatever/', file('filename'));

file — Reads entire file into an array

preg_grep — Return array entries that match the pattern

stereofrog
nice using `preg_grep` and `file` ina 1 line situation, but when dealing with string comparisons you should take in the speed of them 2 functions in comparison with alternative methods.
RobertPitt
@RobertPitt: don't use string functions when you can use a regular expression
stereofrog