Find whole line that contains word with php regular expressions

Borut Flis picture Borut Flis · Feb 3, 2013 · Viewed 15.1k times · Source

I want to search for a word "session" in a text. But I would like to retrieve the whole line in which this word appears. So far I have come up with this.

$pattern="[^\\n]*session[^\\n]*";
preg_match_all($pattern,$content, $matches, PREG_OFFSET_CAPTURE);

But I get an error "Unknown modifier '*'". Any ideas how to make such an regular expression?

Answer

meagar picture meagar · Feb 3, 2013

Your regular expression is missing delimiters, hence your error:

$pattern = "/[^\\n]*session[^\\n]*/";
// or, with single quotes, you don't need to escape \n
$pattern = '/[^\n]*session[^\n]*/';

If I interpret your intentions correctly, you're trying to match zero-or-more not newlines, followed by "session", followed by zero-or-more not newlines.

A simpler (potentially more correct) pattern would be this:

$pattern = '/^.*\bsession\b.*$/m';

That is, from the start of a line (^) match 0 or more of any character (.*), a word-boundary (\b), the word "session", another word boundary, another series of characters, and the end of the line ($), matching over multiple lines (m modifier).

You've sort of reinvented the anchors (^ and $) with [^\n] which is somewhat non-obvious, but missed the word boundaries, which is probably not desired as you're matching any word that contains the word "session". That is, yours would match a line containing "sessions" or "possessions" or "obsessions" or "abcsessionxyz", where mine wouldn't; if this isn't desired, you can remove the \b's yielding /^.*session.*$/m and our patterns will be more or less equivalent.

Here's a proof-of-concept, finding the entire middle line which contains the word:

<?php

$lines ="This is a test
of skipping the word obsessions but
finding the word session in a
bunch of lines of text";

$pattern = "/^.*\bsession\b.*$/m";

$matches = array();
preg_match($pattern, $lines, $matches);

var_dump($matches);

Output:

array(1) {
  [0]=>
  string(29) "finding the word session in a"
}

Your pattern would have found the line "of skipping the word obsessions but".