Making links using keywords

  Charence 23:40 14 Jan 2005
Locked

I've got a page where there are many keywords which I wanted to make links for, but I thought that writing each link could not be the best solution.

e.g. on the PCA Forum, some words are linked to sponsors (the green ones).

How can I do something similar to this? I would like to be able to have a website which will be able to recognise that there is the word "Chicken" and then make the link "Chicken.html" for it.

For a sample of the page I'm wanting to apply this to, take a look at click here

Charence

  Charence 15:38 15 Jan 2005

What do I do if the site is dynamic? It uses PHP and a MySQL database. Do I just change "Chicken" to "<a href="chicken . html">Chicken</a>" in the database?

Charence

  Talented Monkey 18:26 15 Jan 2005

changing the text within the database is very messy time consuming and cumbersome. I assume you already know a bit about PHP and mysql, so ill just give you a brief description with what i would do. then maybe tkae it further.

When you take the webpage text from the database, simply pass it through a function that will replace the words as required. you can sotre the words you wnat to change and the corosponding web address either in the code or a seperate file. like this

chicken,ww.chickenlink/blahb/blah,
rabbit,ww.rabbitlink/blablahb/blahb

In the new function, you will read this info into an array, Start a loop , taking the word and address and simply search and replace each instance of this word with your new link

assume that $webpage_text contains data pulled from your data base which contains "I am a chicken farmer"
$word_to_find is used to store words from your list, and $word_link is the webpage address associated with that .

$webpage_text

$link_created="<a href="$word_link."</a>";

$webpage_text = str_replace($word_to_find , $link_created , $webpage_text);


somthing like that,

Appologies for spelling and typos, any vaugness etc and i hope the code is ok, but i quickly typed this off top of my head have to be out door in 10 mins!

if i had more time i would type the whole thing for you. hope this helps.

  Talented Monkey 00:57 16 Jan 2005

OK heres the code to replace a list of names with thier links within a block of text.


$webpage_text="the is a line of text for the webpage.i love a monkey I love sheep i love horses I love Rabbits";

$replace_links=array("sheep"=>"ww.sheeplink","rabbit"=>"ww.rabbitlink");


foreach($replace_links as $animal=>$newlink){

$newlink="<a href=".$newlink.">".$animal."</a>";
$webpage_text=eregi_replace($animal,$newlink,$webpage_text);

}
echo $webpage_text;

  Talented Monkey 01:10 16 Jan 2005

opps hit post by mistake

the above is a working model to demonstrate replacement. I used an assositve array in end as this is far more flexible, you can change this into a multiple array and have even more fun with word changes, so every instance of rabbit will provide the link and a new word into an alternative. if you want this ill post it later on Sunday, my brain isnt working too good and im liable to make mistake!! i think the code is self explanitory, if you need help to understand it jsut ask and i will.

AnyWay To change this into a function you will need it changed to be like this.

function llink_creator($webpage_text) {

$replace_links=array("sheep"=>"ww.sheeplink","rabbit"=>"ww.rabbitlink");

foreach($replace_links as $animal=>$newlink){

$newlink="<a href=".$newlink.">".$animal."</a>";

$webpage_text = eregi_replace($animal,$newlink,$webpage_text);

}
return $webpage_text;
}

  Charence 18:32 16 Jan 2005

I'm going to try this out and see how it goes :o)

Charence

  Talented Monkey 21:21 23 Jan 2005

...any progress made? i would like to know if you manage this!

thanks

  Charence 12:59 24 Jan 2005

Sorry about this...currently not much progress. I've had exams last week for my A Levels...you will be delighted to know that I have finished them and will be able to get started on this! :o)

Charence

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

Alienware 17 R4 2017 review

These brilliant Lego posters show just what children's imaginations are capable of

Mac power user tips and hidden tricks

Comment réinitialiser votre PC, ordinateur portable ou tablette Windows ?