| 5:05 pm on May 7, 2008 (gmt 0)|
you can use preg_replace [us.php.net] to replace your array of keywords with your link text.
|observer 24 7|
| 5:43 pm on May 7, 2008 (gmt 0)|
Thanks but this does not solve my problem. If i use preg_replace I cannot change Vegetables (ignore case) to:
Pls note that for all matching words (ignoring case), I would like to add a hyperlink but I do not want to change the original word.
So Vegetables would become <a href="/tags/vegetables">Vegetables</a>
vegetables would become <a href="/tags/vegetables">vegetables</a>
vegeTables would become <a href="/tags/vegetables">vegeTables</a>
pls note that the link url is /tags/vegetables . What I am doing is if I find tags in an article, I am linking to the tag page.
Hope I made my requirement clear.
| 7:34 pm on May 7, 2008 (gmt 0)|
preg_replace is still what you want.
using the /i switch in the pattern for preg_replace() will match case-insensitively, so the pattern "/vegetables/i" will match Vegetables, vegETables, VEGETABLES, or VeGeTaBlEs.
If you take it a step further and use the /e switch, you can evalute the replace in a similar fashion to using the eval [us.php.net] function, which will allow you to use strtolower() in your replacement to ensure that the link is lower case.
preg_replace("/vegetables/ei","\"<a href='/tags/\".strtolower(\"\\0\").\"'>\\0</a>\"", $your_string);
| 7:58 pm on May 7, 2008 (gmt 0)|
The eval modifier is a pretty nifty thing to use, however, I feel that unless it's something relatively small (such as what you have here changing the case-so there's no point in changing it) it is cleaner to use preg_replace_callback [php.net] to modify the replacement string. Just for future reference in case you need to manipulate it in a more complicated way than changing the case :)
|observer 24 7|
| 10:54 am on May 8, 2008 (gmt 0)|
Thanks a lot ! It worked with the /e but faced some problems when the string had - in it.
Now trying to make it work with preg_replace_callback
Really appreciate the help !