Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Some Links to some words issue

Tags:

php

I have some words Such as

"Hello this article about games. Our choice is train games. Name of game is My Little Train"

I want to add automatic links as below

"games" » "http://www.mywebsite.com"

"train games" » "http://www.myothersiteadrwesss.com"

"game" » "http://www.my3thsiteadress.com"

I tried it with PHP and str_replace but after my codes

"games" word has 2 links nested ("http://www.mywebsite.com" and "http://www.my3thsiteadress.com" )

"train games" word has 3 links nested (all 3 links)

How can I solve my problem?

finally i want

<a href="http://www.my3thsiteadress.com">game</a>
<a href="http://www.myothersiteadrwesss.com">train games</a>
<a href="http://www.mywebsite.com">games</a>

words count can be change

like image 500
user2006773 Avatar asked Dec 21 '25 02:12

user2006773


1 Answers

I think you should use preg_replace instead of str_replace. That way, you can specify a much stricter regexp to be replaced (Somewhat like a "match whole word"). Here, take a look at http://php.net/manual/es/function.preg-replace.php)

like image 163
Muc Avatar answered Dec 22 '25 15:12

Muc