Customizing SMF > SMF Coding Discussion
Automatically link certain words?
MrMike:
Actually I may have to run it for each post, as I remove keywords from the array once they been used (so they only link x number of occurrences per post). Which means that posts further down the page don't get the keywords linked if they've already been found in an earlier post. But, I can probably just copy the array to a throw-away array used once for each post as well. Let me see what I can do.
--- Quote from: Arantor on May 28, 2012, 09:15:59 PM ---In which case, declare your keywords array as static inside prepareDisplayContext, if it's empty, perform the query. (Since it's static, it'll be preserved between runs)
I get what you mean about running it per post, it was more a suggestion of a possible course of action rather than a recommended course, as it were.
--- End quote ---
MrMike:
--- Quote from: MrPhil on May 28, 2012, 03:45:59 PM ---What if someone miscapitalizes, or obFusCates in order to hide their usage?
--- End quote ---
Correction: I'm using a case-insensitive search, but it doesn't appear to be finding words with capitalization or mixed-case. I'm not sure why it isn't, frankly.
Arantor:
How exactly are you performing that search?
MrMike:
--- Quote from: Arantor on May 29, 2012, 08:59:13 AM ---How exactly are you performing that search?
--- End quote ---
This is the basic search line:
--- Code: ---$post_text = preg_replace('/([\W|\s]+)('.preg_quote($word).')([\W|\s]+)/i','$1<a href="'.$rep_string.'"'.$pp_style.' title="Related pages for \''.$word.'\'">$2</a>$3',$post_text, $rep_count);
--- End code ---
The "/i" should make it case-insensitive.
Arantor:
It should, yes.
However, I think you have more issues than that looking at the code, which may well be the problem. Might I suggest:
--- Code: ---$post_text = preg_replace('/\b(' . preg_quote($word) . ')\b/i', '<a href="' . $rep_string . '"' . $pp_style . ' title="Related pages for \'' .$word . '\'">$1</a>', $post_text, $rep_count);
--- End code ---
You don't specifically have to parenthesise the spacing, you just have to take it into account for matching purposes, and \W or \s+ is essentially either way saying a word boundary, which is \b, and as such you only need to match the actual term itself.
I actually suspect part of the trouble you were having was less about matching capitalisation but not hitting certain combinations of characters. Also note that you don't have to have the | operator when you're inside [] since you're indicating a class, and in fact [\W|\s] means to look for \W or | or \s.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version