Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to write a script that will make it so that when certain words appear on my site they will automatically be converted into hyperlinks. Much like this site does with certain words like - internet .
Right now, I have a database consisting of int(primary), urls (varchar 255), and strings (varchar 255). Basically I just need to know how to cross reference all the strings in the table, with the body of my page, then if there is a match, use the int number to replace it with the url.

Okay the site made a liar out of me. I could have sworn that the word "internet" turned into a link in one of my other posts.

Okay, I see now. I was viewing it in Opera, and it only shows in IE. Okay, preferably I would like non IE exclusive solution to the original question.

I can't think of any non-brute force way to accomplish this. Simply loop through the records in the database, and replace each instance of STRING with a link to the URL.
With lots of records in the database, or large text files, this is going to kill your resources (hence the hang here on computing.net between loading a page and being able to scroll down). If it were me, I would actually have the script output a static html file with the links, instead of running the script on each page request. You could rebuild the file as often as you like, without it taking much in the way of resources.
$out_file=file_get_contents("myfile.txt");
$result=mysql_query("SELECT * FROM LINKS");
while ($myrow=mysql_fetch_array($result)
{
$out_file=preg_replace($out_file, $myrow['string'], "[A href='$myrow[url]']$myrow[string][/A]");
}
print $out_file;Note that you should take care not to have strings that are subsets of other strings. For example, the algorithm above would mess up if you had two records in the database, one with "Great motherboards" as the string and the other as "motherboard" as the string.
Sorry I couldn't think of a more elegant solution.
-SN

Using static documents is certainly the fastest way, but string matching your database regularly isn't very dynamic (and when you add content it creates an extra step). Also, It could pollute your data - what happens when you no longer want a keyword as a link or one link no longer works? Then you undo your work. Anytime you have to query an entire database, parse all your documents, etc. is a time to think again.
Let's say you stay with replacing the keyword string with a link. That's perfectly fine for most cases. String functions and regex work well in PHP and are not necessarily slow (a relative term to be sure).
So create a script that parses a given set of text according to string matching by keyword, where keywords are replaced by anchor tags with the keyword. First, you need your keywords and URL's available to the script and associated. Use an array with keyword=URL, or even better two arrays of id=key and id=URL, where id(0)=id(0). Consider making this structure an include file.
Next, encapsulate the text segment in a data structure. One variable would be OK, perhaps an array (the array might be faster, I would have to look into that).
Once the text segment is local to the script, search it with the keyword. Look for one keyword at a time. Set a threshold of keywords allowed to be searched (I would make this all one function with a few arguments) so that the searching can stop after so many keywords or number of matches has been made. This will keep your script's execution time trim.
This way, you only alter a copy on-demand. The slowest part should be the database query (but it depends on your keywords - how many keywords and how many matches).

anonproxy's suggestion of limiting both the number of matches and the number of keywords is probably a good one. You'll want to read up on how to make regular expressions a little more efficient (don't use backtracking, make sure any variables you're using are not evaluated on each iteration, etc.)
I should have explained my idea a little more clearly...You would probably have two different files, one with the original content, and another with the output of the php script. Each time you wanted to regenerate your page, you would work from the content file, so you wouldn't have to do anything special to delete links if you wanted to...Just rerun the script and it will re-render the output file. I don't believe your data would get polluted.
To get the best of both worlds (speed of static pages and dynamic content of the PHP script), you could have the PHP script compare the timestamps of the content file and the php output file. If the content file has changed since the the output file was created, then re-render the output file. Otherwise, just read in the output file and send it to the browser.
This approach requires some database maintenance overhead (because although the content file may be older than the output file, the database may have changed since the output file was generated.) I would probably maintain a file with a date/time that indicates the time database was last changed. You would either have to manually maintain it (yuck), or, if you do all your database inserts, deletes, and updates via PHP, your database management PHP scripts could maintain it for you. There are a million ways to keep the content and the database in sync, just pick one that seems intuitive.
-SN

Thanks a lot for you guys' help. I haven't had a chance to implement the ideas, but I'm sure they will work out nicely.

I am currently working on a solution to handle this type of process.
As an example, view http://www.mychiroweb.com/article/87/Vertebral_Subluxation_Complex/ and look on the right side navigation.
Currently, I am only listing the owrds contained in the glossary, but hope to have them hyperlinked within the document itself, very soon.
(the site isn't quite done yet, so please excuse the mess)

![]() |
cannot view geocities or ...
|
Website CSS problems
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |