To give the background, this code will be used to search messages posted to my trading card game forums, and turn all card titles (a few thousand possibilities, the full list is pulled from a database) into links to that card's image and text.
In the code I'm currently using, the line that does the replacement looks something like this:
Code: Select all
$message = str_replace($match, $link, $message)
$match is an array containing all the cardnames in order, and $link is an array containing the same card names, surrounded by the code that makes them links.
This works OK, but could be better. For example, take the following pair of sentences -- the matches I want are bolded.
"i prefer using
sting in this deck, as it has shown some great results in playtesting"
"
Sting rules!"
The current code would find only one match as follows. (To correct this, I'd need to make it case-insensitive.)
"i prefer using sting in this deck, as it has shown some great results in playtesting"
"
Sting rules!"
But, once I do that, I get too many matches:
"i prefer using
sting in this deck, as it has shown some great results in playte
sting"
"
Sting rules!"
So now, I need to prevent it from matching partial words. (Checking the characters before and after the match seemed the logical way to do this, please let me know if there's a better approach!)
Just to clarify, I know how to put together a regex that would match "Sting" as described above, but when I replace that word with the variable $match (remember that "Sting" is one of a few thousand card titles that need to be checked!) I can't make it work.