SunQuest
 
           General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old December 4th, 2002, 10:07 PM
Scurvy Scurvy is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 3 Scurvy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Automatically adding lnks to selected words in an article

Is there a way to have php scan through a document to find certain words listed in a database and then automatically attach a hyperlink to them?

Example:

Article contents:
Hi, this article is about Osiris. He was a great man in ancient Egypt blah blah blah

I want Osiris to link to another page with a listing of all pages about Osiris. Also, Egypt will link to another page with a listing of pages about Egypt.

This would also be used for images where the link would be from a word in the article to an image file.

I'm guessing this would be an advanced level situation and I have no idea how it should be done so any help would be appreciated.

Scurvy

Reply With Quote
  #2  
Old December 5th, 2002, 06:10 AM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
This is pretty simple is do.

There are a number of ways you can do this. I will give you an example of how this can be done using link entries that are located in a flat text file ->

Code:
Egypt|yourpage.com/egypt.html
Osiris|yourpage.com/osiris.html


Ok, the above would be the link_data.txt file. This file will hold all your link data, or data you want to link from your articles. The data to the left of the pipe will be the link names and the data to the right of the pipe is the actuall links.

Each entry is on its own line in the file (no whitespace inbetween lines)

PHP Code:
function link_data$string ) {
    
$data file'link_data.txt' );
    foreach ( 
$data as $val )
        {
            
$chunks explode'|'trim$val ) );
            
$find trim$chunks[0] );
            
$replace '<a href="' trim$chunks[1] ) . '">' $find '</a>';
            if ( 
strstr$string$find ) )
                {
                    
$string str_replace$find$replace$string );
            }
    }
    return( 
$string );



The above is your function. Call the function as so ->

PHP Code:
 $string 'Hi, this article is about Osiris. He was a great man in ancient Egypt blah blah blah';

$data link_data$string ); 


---------------------------------------------------

There are many other ways this can be done also, this is only a simple way of doing it. If you need the words to be case insensative, then replace the str_replace() with preg_replace() or other reg exp.......
__________________
~ Joe Penn

We work for free to help make this a valuable resource on the internet. Do you appreciate the help - did we provide help that will help you prosper and help that has contributed to sharpening your current skill set?

Show your appreciation and purchase something from our Amazon Wishlist's - it's simple and a great way to say thank you.




Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Automatically adding lnks to selected words in an article


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway