Page 1 of 1

making words hyperlinks

Posted: Sun May 21, 2006 6:20 am
by Addos
Hi,
I need to have various words throughout a site that I’m building made clickable. For example lets say I have AXO and ROB how can I make these become automatic links or even pop up div’s when moused over as they turn up in the various places throughout the site. Is there some regular expression that I need to use? I have never done this at all and would appreciate any advice as to how I can set this up. Keep in mind I’m still very new this all this.
Many thanks
Brian

Posted: Sun May 21, 2006 7:02 am
by s.dot
I have never done this before, but I would imagine this is the logic.

1. Build an array of words to be hyperlinked. and links to go to
2. Get the page text content into a variable. IE: $textcontent
3. Perform a str_replace() on the content

perhaps something like this

Code: Select all

$words = array(
   'word1' => 'http://www.word1.com',
   'word2' => 'http://www.word2.com',
   'word3' =>'http://www.word3.com'
);

$content = $database['content']  // or however else you grab the content

foreach($words AS $k=>$v){
  $content = str_replace($k,"<a href=\"$v\" target=\"_blank\">$k</a>",$content);
}

Posted: Sun May 21, 2006 7:19 am
by Addos
Thanks for you reply.
I wonder if the heading was misleading in that I’m only looking for specific words to become active for example when I type str_replace() somehow on this forum page it becomes active.
Is the code you suggested still the same thing I’m looking for?
Thanks
Brian

Posted: Sun May 21, 2006 7:26 am
by s.dot
Yes. You will have to specify which words you want to become active in the array.

Unless you're looking for the bbcode [ url ] function(s)?