Everytime "this word" appears it needs to be linked

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I have a question and I would like it solved by tomorrow. That's when I leave for Honduras:).

I am working on web based software (educational) and the client has a glossary of difficult words used in the test questions and answers. They need every word in that glossary to, whenever it appears; be linked to the glossary definition. Is there a way I can do that globally? I know ad ware does the same thing to your computer but is there a script that can do it?

Here's an example of what they want done...

The small community prospered during the gold rush.

The word "prospered" needs to be linked to example.com/glossary/prosper

We could go and link every word manually but that would take hours.

Thank you so much for the help!!

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

You'll want to run a filtering script that replaces, perhaps by regex, the keywords as it reads them off a list (array, database, whatever). This should be fairly simple... loop through the glossary and run a preg_replace on the content string each time, adding tags etc. With this, you have three options:

1) Depending on your CMS's editorial process for adding content, you may want to invoke the script each time something is posted. Maybe this works best for blogs or forums.

2) If your CMS caches content, then have the script run at every cache alteration. That way viewers will see the hardcoded links whereas the data store will have virgin text.

3) Probably the worst solution is to run the script to parse all output text every time it's viewed. If you're in a controlled network, that could be ok.

On some sites where I've seen this done, I've noticed that the 'glossary' links load after everything else has rendered. That probably means there's some Javascript trickery going on as well. It's basically the third option above, but with the stressful work delegated to JS instead of PHP -- the stressful part is done by the browser, not your server. I guess in this case, PHP simply matches the glossary with the output string and passes an array of relevant words/urls to JS, which does the replacement work.

Iirc, Drupal has a glossary.module or definitions.module or something like that. So I'm sure there must be scripts written already for this. Try pear.php.net, phpclasses.org and hotscripts.com.

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

in php you can do:

(haystack is all the text that you want to sort through, and the needle is the word you want to replace it with)
$haystack = "make this needle a link";
$haystack = str_replace('needle','needle',$haystack);
(str_replace is case sensitive, if you have php5 you can use str_ireplace, or have multiple str_replaces)
echo $haystack;

the other option, preg_replace() is a little more complex, this site: http://regexlib.com/Default.aspx can teach you a little more what Regular Expressions (RegEx) are.

demonhale's picture

He has: 3,278 posts

Joined: May 2005

Here my piece...
get a freeware program that batch replaces any keyword you assign with a new code: (e.g. replace "wake" with "wake") it replaces all of that word in the code with the new one.. You can even replace a bunch of seperate html or css or whatever file with text...
You do it only once... And im sure there is this kind of software...

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

I suggest using regex over str_replace if the haystack contains meta information like HTML tags. Using a comprehensive regular expression, you can avoid accidentally replacing strings in HTML tags that may collide with words in the glossary. Suppose you have the word "block" in the glossary, the identical substring in

will be affected, if you use str_replace. Of course, if the haystack is pure, then str_replace may be a better choice due to efficiency.

Smiling

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Thanks for the help guys! You made it a lot easier.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.