Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database driven keyword link generator

Status
Not open for further replies.
Aug 23, 2004
174
US
Here is what I'm looking to do.

I have a table of keywords with associated links. I want to search through the copy on a page and replace those keywords with the associated link.

I was thinking javascript would be the best approach but was looking for a little input. Anyone have any ideas?

Joe
Web Developer
 
you could do this with pho if i understand you correctly, and you want to parse a page and replace all found keywords with a link.

You'd need to use fopen() to read the source, exlpode() to create an array of words and preg_replace() to replace the source words with any matching databse links.

Thats provided its currently static cintent, if the content is already in the database too, you could probably get alot more creative with teh retrieval queries if it's mysql.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hi

If you are doing it in JavaScript, then you will have to transfer the whole content of the database table into the visitor's browser. Do you really want that ?

Supposing no, there are still two ways :
[ul]
[li]do it with PHP[/li]
[li]do it with MySQL stored procedure[/li]
[/ul]
The biggest question now is what "copy on a page" means ? If the pages are generated by some framework/CMS/whatever which stores the page contents in the same database, then I would probably try the stored procedure approach.

Otherwise, [tt]str_replace()[/tt], [tt]preg_replace()[/tt] or [tt]preg_replace_callback()[/tt].

Additionally you could take a look at thread434-1582160 . Not the same situation, but there are keywords in database too.

Feherke.
 
It really depends on how you want and are able to approach this.

JS would need to be placed inside the page, which means the change could be also done directly by you without any coding, just a normal search and replace from a text editor. Since you ve access and can modify the pages source code.

Assuming you don't have access the page in that manner then a server side approach would be suggested.

PHP could read and then parse the page before delivering it to the client without actually modifying the original page's content directly.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The "copy" is from a mysql database. The thing I was worried about was if I have say 100 keywords, and every page has to be checked every time it's loaded, would this slow down loading times too much?

Definitely not going the javascript route. I'm thinking about setting up a cron job to run a script every night and update all of the links in the copy.

Thanks for all the replies.

Joe
Web Developer
 
I have a basic idea for how I can tackle this, but I've run into another issue.

The copy from my database is formatted with html and some of the keywords and phrases are already linked. How would I make sure the tag isn't already a link?

Joe
Web Developer
 
The easiest method assuming a link can only ever contain one keyword is to look at the characters immediately following the keyword. If they are "</a>" 5then you know that keyword is already inside a link.

You'd need to use the strpos() function to look for the keywords and then use the substr() function to get a substring containing the keyword plus the following characters to check for the closing link tag.

A more sophisticated way would be to use a regular expression than can identify the words that are not inside link tags, and change those.

Sadly my regular expression knowledge is quite limited so someone else may need to chime in with the proper expression to get it to work.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top