Question

I'm trying to replace certain string occurrences in a text file with links. Some of the entities consist of more than one word, i.e. they are strings with spaces in between. I'm essentially looking for a regex that takes punctuation into account and is case insensitive. My 'solution' just seems to go around in circles and my regex knowledge just isn't good enough for this :(

$pattern = '/[ ._*&^%$#!?!]Anthony Anderson[ ._*&^%$#!?!]/i';

$replacement = '[[Tony Anderson]]';

$subject = 'Anthony anderson. (born August 15, 1970) is an actor, comedian, and writer. He has starred ... the ANTHony Anderson, as well as in Fox sitcom. anthony Anderson. Anthony aNderson? !Anthony AnderSon. Anthony    Anderson. . anthony
anderson. Anthony Andersoneon should not match neither should Santhony Anderson ';

echo preg_replace($pattern, $replacement, $subject, -1 );

Got me this far:

Anthony anderson. (born August 15, 1970) is an actor, comedian, and writer. He has starred ... the ANTHony Anderson, as well as in Fox sitcom.[[Tony Anderson]][[Tony Anderson]] [[Tony Anderson]] Anthony Anderson. . anthony anderson. Anthony Andersoneon should not match neither should Santhony Anderson

But I'm after:

[[Tony Anderson]]. (born August 15, 1970) is an actor, comedian, and writer. He has starred ... the [[Tony Anderson]], as well as in Fox sitcom. [[Tony Anderson]]. [[Tony Anderson]]? ![[Tony Anderson]]. [[Tony Anderson]]. . [[Tony Anderson]]. Anthony Andersoneon should not match neither should Santhony Anderson

What am I missing?

Était-ce utile?

La solution

I think that you want to use the word boundary to match, \b. This includes non-word characters, but also the beginning or end of the string:

/\bAnthony Anderson\b/i
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top