making words hyperlinks

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
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

making words hyperlinks

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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);
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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)?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply