homepage Welcome to WebmasterWorld Guest from 54.145.183.190
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Dynamic Linking in content
woldie

10+ Year Member



 
Msg#: 8371 posted 3:19 pm on May 25, 2005 (gmt 0)

Hi,

Not posted here for donkey's years! :o)

Anyway I have a query. What I am trying achieve is to do some on the fly linking. What I have is some content from a DB, and within the content if a particular word appears then I would like to insert a link to the product page.

Now I'm not quite sure the best way of implementing this. whether I should create a keywords table in the DB and then compare the content with the keywords table?

Any suggestions or examples of code?

Thanks

Woldie.

 

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 8371 posted 10:51 pm on May 26, 2005 (gmt 0)

That sounds like a fair approach to me. Why not just modify the content in the database to include the link?

ergophobe

WebmasterWorld Administrator ergophobe us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 8371 posted 12:42 am on May 27, 2005 (gmt 0)


Why not just modify the content in the database to include the link?

Because then it's harder to change them on the fly.

I presume it's a small set of links and words (like a few hundred at most)? Since you're going to need to get all the words into an array for every page, I would probably just store it in a CSV file, upload the words and the replacement link to an array, and then use preg_replace or str_replace, sending it the array of word, the array of replacement links and the text from the DB.

str_replace has the advantage of speed, but the disadvantage of not being able to distinguish whole words from strings within words.

Stormfx

5+ Year Member



 
Msg#: 8371 posted 7:07 am on May 27, 2005 (gmt 0)

Actually, whole/partial words can be easily distinguished. Simply add a space to the start and end of the search string and then make sure to add them back into the replacement string. On a query such as mentioned, those microseconds are going to count.

ergophobe

WebmasterWorld Administrator ergophobe us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 8371 posted 3:12 pm on May 27, 2005 (gmt 0)

Sorry, but it's not that easy. For example

Word wordblah blahword word blah word, blah word
word blah blah word.

If you search for " word " wrapped in spaces, you get one match, not five as you would using a regex to grab whole words.

Stormfx

5+ Year Member



 
Msg#: 8371 posted 4:14 pm on May 27, 2005 (gmt 0)

I understand your point. Especially if there's a need to count the total number of words or perhaps reuse the words themselves, for example in the event of an case-insensitive search where we want the case to be restored. However, if the search isn't that specific, it is just as easy I mentioned. Using str(i)_replace on an array of keywords will replace every instance of the word with the replacement string. The only areas where this might be a problem is if the search term is at the first or last of the sentence (case or followed by puncuation).

I wasn't trying to say that this situation doesn't call for regular expressions, just that it's always best to evaluate the need before using them. :)

woldie

10+ Year Member



 
Msg#: 8371 posted 11:59 am on May 31, 2005 (gmt 0)

Thanks for the responses.

This looks quite complex than first imagined. Does anyone know of any example scripts out there, just to get a rough idea?

Thanks

W :O)

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