I have a module on my self-written CMS called 'autolinks'. You add items to the autolink table in my database, in the form 'word - url' (eg, 'BBC' maps to '
http://www.bbc.co.uk'). When submitting an article, I select all of the words in the autolink table, loop through them, and run a search and replace on the article text, eg, if it finds the word BBC, it links it to the BBC url.
In retrospect after writing this function several years ago, I would modify it like this:
- make it non-destructive. Eg, don't modify the original article text. If I want to delete an autolink from my site, it won't affect the existing articles, and it will be a massive pain to remove them all from articles if I ever needed to. I might have added a column to my articles table to contain the version of the text with the autolinks, just in case.
- make it smarter. At the moment we don't use many other links within the articles, but my code doesn't do anything smart like check if the matched text is already a link, so it could lead to some messy situations.
- with relation to my first point, when editing articles I have to disable the adding of autolinks, since this could lead to duplicate linking when my code finds 'BBC' a second time and tries to link it again. This means that if I edit an article and add in a new word which is also an autolink, it won't be autolinked in the edited post because the function isn't run a second time. If I'd done as I said above and stored two versions of the text, I could load this 'raw' version of the text when editing, so that I could process autolinks at all times. I suppose the alternative would be to run the function at runtime, but that's slow and inefficient.
- make it dynamic. As you identify, these systems should work retroactively - eg, if you add a new glossary item, it should be reflected on all existing articles. Simple - do the above but in reverse. Loop through all your articles when you add a new glossary item, and do the search and replace there instead. A bit more intensive on your server, but it should give the results you want. And if you're smart with the query you use to get data (eg, "SELECT * WHERE article_text LIKE '%expensive automobile%'" or something) then you should only grab the rows you need to change.
Your glossary idea is basically the same - you need to define a table that contains all of your glossary items and the relevant links, then learn from what I've posted above and apply it to your system. Good luck!