Automatically creating links to a glossary entry?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
onyxmoron
Forum Newbie
Posts: 6
Joined: Wed Apr 29, 2009 9:12 pm

Automatically creating links to a glossary entry?

Post by onyxmoron »

I was thinking about how to add "glossary" links to website pages automatically for specific sequences of words. Like, say I want every occurrence of the phrase "expensive automobiles" on a website to automatically become a link to a glossary definition of that word (on another page), so on a page with this content:

Code: Select all

<p>
For people who like to drive and are rich, expensive automobiles are a good choice.
</p>
it would automatically insert a link to a glossary entry around "expensive automobiles". (This is just an example.) In this way, if I later added a glossary entry for the phrase "good choice", every occurrence of that phrase could become a link automatically and I wouldn't have to go back and manually insert it everywhere that it occurred.

The way I thought of is to load the actual page content in a string variable and use preg_replace to find all occurrences of things that are in the glossary and replace them with a "linked" version, but I don't know if that would be efficient or a bad way to do it?
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Automatically creating links to a glossary entry?

Post by Yossarian »

I managed this in JS once, saves going through all your content and changing the markup every time a new glossary entry is made - or grrr, edited.

It is old code lying in one of the other places and I was in one of my other guises ... Glossary questions and JS
onyxmoron
Forum Newbie
Posts: 6
Joined: Wed Apr 29, 2009 9:12 pm

Re: Automatically creating links to a glossary entry?

Post by onyxmoron »

Yossarian wrote:I managed this in JS once, saves going through all your content and changing the markup every time a new glossary entry is made - or grrr, edited.Glossary questions and JS
If I had to go back and do them all by hand I'd probably just never do it at all. :(

Thanks for the link! I don't really know how to do Javascript, I'm trying to use this as a learning opportunity with PHP since I'm obviously a newbie. :lol:
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Automatically creating links to a glossary entry?

Post by mattpointblank »

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!
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Automatically creating links to a glossary entry?

Post by Yossarian »

@mattpiontblank

I did pretty much the same on my own CMS too, after what I learned trying to do that JS trick.

I would add some other stuff to your list that I learned (from bitter experience).

Keep 2 copies of the article (text). I) just text as the user submitted it, and 2) as autolinked items (ie its the text but now with links in it). (in fact 90% of the time I cache that part of the file)

If not when the user comes back to edit the article you have to rely on strip_tags() which is not that reliable.

If I was doing it again I would make the links redirect through a central page that monitors what is being clicked ( and can then check for 404s).

I extended the links idea too from "autolinks" as you term it, to "text-translate" where I then made internal links to known features like "people" so when the users write "you should contact George Philips "it becomes "you should contact <a href=/people/GeorgePhilips>George Philips</a>".

That is probably more like what the OP wants as the OP does not dig JS.
onyxmoron
Forum Newbie
Posts: 6
Joined: Wed Apr 29, 2009 9:12 pm

Re: Automatically creating links to a glossary entry?

Post by onyxmoron »

Whoa did I mention I'm relatively a newbie? :oops: haha

Thanks for helping. You guys brought up some of the things I was worried about, like inserting links into terms that are already linked.

I thought about it more and realized this may not be the best way to do it. I posted a new message here with another idea I had:

viewtopic.php?f=1&t=99500

This way wouldn't do anything to existing content but it might work better for my site.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Automatically creating links to a glossary entry?

Post by mattpointblank »

Maybe a combination of both methods? Doing it on-the-fly is a bad idea, it's slow and heavier on the server. Write some good code that produces good results (and doesn't add links a second time etc) and add a second text field to your table with the edited version there, so your original text isn't touched. Embedding [link|website.com] type codes would definitely make the search/replace function easier to manage with some smart regular expressions, with less margin for errors.
onyxmoron
Forum Newbie
Posts: 6
Joined: Wed Apr 29, 2009 9:12 pm

Re: Automatically creating links to a glossary entry?

Post by onyxmoron »

mattpointblank wrote:Maybe a combination of both methods? Doing it on-the-fly is a bad idea, it's slow and heavier on the server. Write some good code that produces good results (and doesn't add links a second time etc) and add a second text field to your table with the edited version there, so your original text isn't touched. Embedding [link|website.com] type codes would definitely make the search/replace function easier to manage with some smart regular expressions, with less margin for errors.
Thanks mattpointblank. I ended up doing basically what you said, writing the original pages with "markup" then saving a "parsed" version for it to load instead so it doesn't have to parse each one on the fly. I haven't written a script tor reparse every file in the folder at the same time so I guess that's next. But I didn't put them in a table, I have them saved as html files. Do you put the actual content of a page into a table? (I only have things like title and keywords in tables, the actual content is just a regular file that gets included.)
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Automatically creating links to a glossary entry?

Post by mattpointblank »

It's up to you - if your articles aren't too long, then you may as well store them in the database too - if you're already querying it for title/keywords, it won't add much to the load time to grab the text as well, and this means you can do more with it (for example, a 'print article' page which shows just the text, minus the layout/images).
Post Reply