Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

replacing words with links to urls

php replacing words with links to urls

3:37 pm on May 7, 2008 (gmt 0)

5+ Year Member

Friends and php gurus,
This is what I am trying to do.

replace all occurrences of the words in an array to links to their tag pages (urls).

So for eg:

$tags = array("fruits", "vegetables", "fiber");

I would like to replace:

$str = "I like fruits. Vegetables are not my favorite. ";


I like <a href="/tags/fruits">fruits</a>. <a href="/tags/vegetables">Vegetables</a> are not my favorite.

PLEASE note that I would like to ignore the case while comparing/matching and would like to keep the same case in the actual string. Check how Vegetables got changed to a link to vegetable regardless of the case. So my source string does not change (in html display), only the links are added to their tag urls.

I tried using str_ireplace but could not find a way to remmember the original word. Any help will be appreciated.

thanks !

5:05 pm on May 7, 2008 (gmt 0)

10+ Year Member

you can use preg_replace [us.php.net] to replace your array of keywords with your link text.
5:43 pm on May 7, 2008 (gmt 0)

5+ Year Member

Thanks but this does not solve my problem. If i use preg_replace I cannot change Vegetables (ignore case) to:
<a href="/tags/vegetables">Vegetables</a>

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)

10+ Year Member

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)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member

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 :)
10:54 am on May 8, 2008 (gmt 0)

5+ Year Member

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 !