I'm not sure what are the other things you have in the regex for, but here's the one I would use:
$pattern = '/\bby\s(?!.*\bby\b).*?$/i';
\b
is word boundary and will match only between a \w
and a \W
character or at string beginning/end.
by
matches by
literally.
\s
matches a space (also matches newlines, tabs, form feeds, carriage returns)
(?!.*\bby\b)
is the negative lookahead and will prevent a match if there is another word by
ahead.
.*?$
is to get the remaining part of the string till the end of the string.