Reply
Best Code to use for in-text links
Old 05-22-2006, 07:40 PM Best Code to use for in-text links
sepple's Avatar
Super Talker

Posts: 141
Trades: 0
I want to put links in my text such as many websites do based on certain words.

For example:

if a sentence on my site says "Those keychains are real great"

I want the word "keychains" to have a link on it.

I only need to do this in one block of text that will be in the same spot on each page.
I know I can do it in php by just exploding the block of text and searching for the word and then placing the link on it appropriately but I was just wondering if there is an easier way.

Thanks,
Stephen
__________________
Thanks
Stephen
sepple is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 05-22-2006, 11:13 PM Re: Best Code to use for in-text links
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,556
Name: Keith Marshall
Location: West Hartford, CT
Trades: 0
Create a csv file (you can export from a spreadsheet app) with the text , link

Code:
keychains,keychains.html
headlights,lamp.html
door,door.php
car,vehicle.htm
Use this function:
PHP Code:
function text_to_links($text) {
  
// Include our csv file...
  
$linkfile file('textlinks.csv');
  
  
//$links = explode(',', $linkfile);  print_r($links);
  
  
for($i 0$i count($linkfile); $i++) {
  
    
$links explode(','$linkfile[$i]);
    
    if (
$links[1] != '') {
      
$text preg_replace('/(' $links[0] . ')/im''<a href="' $links[1] . '">$1</a>'$text);
    }
  }
  
  return 
$text;

Then pass the text as follows...
PHP Code:
// This is the text blob in which we will convert to links...
$textblob 'Try our nifty new keychains when driving your car. Headlights and car door accessories are also available!';
  
// Echo the converted text wherever you want it placed...
echo text_to_links($textblob); 
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is online now
Reply With Quote
View Public Profile
 
Old 05-23-2006, 06:10 PM Re: Best Code to use for in-text links
sepple's Avatar
Super Talker

Posts: 141
Trades: 0
Wow, you are amazing sir
__________________
Thanks
Stephen
sepple is offline
Reply With Quote
View Public Profile
 
Old 05-28-2006, 05:50 AM Re: Best Code to use for in-text links
Ultra Talker

Posts: 251
Location: Belgium, Antwerp, Zoersel
Trades: 0
Very nice, but there's still one tiny little problem with your script: if there's a word in your text, that's not car, but has car in it (cars, for example), it will be changed into a link, but only partly:

e.g.:
Code:
cars -> <a href="vehicle.htm">car</a>s
To avoid things like that, just modify the regex a little:

PHP Code:

$text 
preg_replace('/(' $links[0] . ')/im''<a href="' $links[1] . '">$1</a>'$text);

//becomes

$text preg_replace('/\\s(' $links[0] . ')\\s/im'' <a href="' $links[1] . '">$1</a> '$text); 

Last edited by Orodreth; 05-28-2006 at 05:53 AM..
Orodreth is offline
Reply With Quote
View Public Profile Visit Orodreth's homepage!
 
Reply     « Reply to Best Code to use for in-text links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.34606 seconds with 13 queries