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).