homepage Welcome to WebmasterWorld Guest from 54.163.72.86
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
replacing words with links to urls
php replacing words with links to urls
observer 24 7




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

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. ";

to:

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 !

 

jezra




msg:3644023
 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




msg:3644063
 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:
<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.

jezra




msg:3644164
 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);

eelixduppy




msg:3644194
 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




msg:3644587
 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 !

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved