Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Auto Replace words in text with individual links
Old 07-02-2006, 04:15 PM Auto Replace words in text with individual links
Skilled Talker

Posts: 61
Trades: 0
I'm creating a collection of exercise descriptions stored in a mysql DB. Each exercise will have an ID by which it's called from a list to view all info, and then a name, video file name and description.

What I want to do is automatically find any exercise names in the descriptions that match any in the DB and automatically replace them with links to that exercise.

Sample listing:


Exercise A name

Exercise A description..... See also Exercise B Name.


So what I want to happen is the Exercise B Name in that description to be automatically enclosed in a link like <a href="exercises.php?exerciseID=whatever"></a>

I know how to replace a given string with another, but I don't know how to find matches in the DB and replace multiple matched strings each with a different link.

Any ideas? I haven't been able to find any kind of tutorials for this. Thanks in advance.
gregory is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-02-2006, 05:09 PM Re: Auto Replace words in text with individual links
Super Talker

Posts: 144
Trades: 0
ok, i'm not quite certain the order of things. where is the originall list coming from?

Quote:
Exercise A name

Exercise A description..... See also Exercise B Name.
all of this information should be in the db.. there won't be a need to go back and replace names with id's otherwise.

but if you cannot make the necessary changes, you can use a regular expression to find and replace names w/ links. i would look into 'preg_replace()'

something like this:

Code:
$str_of_excercises = "Exercise A name \n Exercise A description..... See also Exercise B Name";
while ($row = mysql_fetch_array($rs))
{
 preg_replace(
  "/". $row["name"] ."/ig" ,
  "<a href=\"excercises.php?id=". $row["id"] ."\">". $row["name"] ."</a>" ,
  $str_of_excercises
 );
)
echo $str_of_excercies;
be carefull for duplicate replaces with this method...

personally, I would store all this in the db w/ the related links and have a nice and easy loop w/ xhtml structure... something like this

Code:
$rs = mysql_query("SELECT * FROM excercises ORDER BY name");
while ($row = mysql_fetch_array($rs))
{
 echo "<div class=\"excercise\">\n";
 echo "<h1 class=\"title\">". $row["name"] ."</h1>\n";
 echo "<p class=\"description\">". $row["description"] ."</p>\n";
 
 echo "<div class=\"related_links\">";
 
 $rsRelated = mysql_query("SELECT * FROM related_excercises WHERE idExcercise = ". $row["id"] ." ORDER BY name");
 
 if (@mysql_num_rows($rsRelated) > 0)
 {
  echo "See also, ";
 
  while ($row2 = mysql_fetch_array($rs))
   echo "<a href=\"excercise.php?id=". $row2["idExcercise"] ."\">". $row2["name"] ."</a>\n";
 
  echo "</div>\n";
 }
 
 echo "</div>\n";
}
__________________
create.vibe

Please login or register to view this content. Registration is FREE

Last edited by createvibe.com; 07-02-2006 at 05:14 PM..
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 07-04-2006, 02:52 PM Re: Auto Replace words in text with individual links
Skilled Talker

Posts: 61
Trades: 0
Thanks! I'll check this out.
gregory is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Auto Replace words in text with individual 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.11816 seconds with 12 queries