Dynamically adding hyperlinks

 
Post new topic   Reply to topic    Aprelium Forum Index -> General Questions
View previous topic :: View next topic  
Author Message
RTAdams89
-


Joined: 06 Nov 2005
Posts: 102

PostPosted: Fri Mar 02, 2007 4:16 am    Post subject: Dynamically adding hyperlinks Reply with quote

I am looking for a script (javascript or PHP) that I can insert into one of my web pages that will search the text on the page and add hyperlinks to key words. Anyone know of a script like this?
Back to top View user's profile Send private message
abyssisthebest
-


Joined: 30 Jun 2005
Posts: 319
Location: Boston, UK

PostPosted: Fri Mar 02, 2007 8:21 pm    Post subject: Reply with quote

you can try and make a php (mysql) database with the words in it with the links, so foe example

key_word and href as the colums

then try a code like

Code:
<?php
mysql_connect("localhost","username","password");
mysql_select_db("db");

$query = mysql_query("SELECT * FROM `links`");

while ($row = mysql_fetch_assoc($query))
{
     $text = str_replace(); // cant rember the syntax...
}

print $text;
?>

that would work but it will need some fine tuning...
_________________
My online Portfolio
Back to top View user's profile Send private message Send e-mail MSN Messenger
cmxflash
-


Joined: 11 Dec 2004
Posts: 872

PostPosted: Fri Mar 02, 2007 8:39 pm    Post subject: Reply with quote

abyssisthebest wrote:
you can try and make a php (mysql) database with the words in it with the links, so foe example

key_word and href as the colums

then try a code like

Code:
<?php
mysql_connect("localhost","username","password");
mysql_select_db("db");

$query = mysql_query("SELECT * FROM `links`");

while ($row = mysql_fetch_assoc($query))
{
     $text = str_replace(); // cant rember the syntax...
}

print $text;
?>

that would work but it will need some fine tuning...


This code would make the word abyss link to Apreliums site:

Code:
<?php
while ($row = mysql_fetch_assoc($query))
{
     $text = str_replace("abyss", "<a href='http://www.aprelium.com'>abyss</a>", $text);
}
?>
Back to top View user's profile Send private message
RTAdams89
-


Joined: 06 Nov 2005
Posts: 102

PostPosted: Fri Mar 02, 2007 9:23 pm    Post subject: Reply with quote

I was hoping for something non-dependent on SQL. Ideally it would be just a script I could insert into the head of a document and not require any other resources.

I want to be able to define a list like:

mountain = "http://locationofmountain.com"
river = "http://maps.com/river/"

So that when ever "mountain" is found on the page it is hyper linked to http://locationofmountain.com.
Back to top View user's profile Send private message
abyssisthebest
-


Joined: 30 Jun 2005
Posts: 319
Location: Boston, UK

PostPosted: Sat Mar 03, 2007 11:41 am    Post subject: Reply with quote

thanks, cmxflash...


you could try is as an array....

Code:

$words = array(
                        "areplium" => "http://www.aprelium.com"
                        "my site" => "http://www.shroodoo.co.uk"
                      );

_________________
My online Portfolio
Back to top View user's profile Send private message Send e-mail MSN Messenger
cmxflash
-


Joined: 11 Dec 2004
Posts: 872

PostPosted: Sat Mar 03, 2007 1:07 pm    Post subject: Reply with quote

Here's a simple script that will read the contents of index.txt and replace some keywords with links.

Code:
<?php
$html = file_get_contents("index.txt");

$replace_urls = array(
"abyss",
"jolt",
"cnn"
);

$replace_with = array(
"<a href='http://www.aprelium.com/'>Abyss</a>",
"<a href='http://www.joltcola.com/'>jolt</a>",
"<a href='http://www.cnn.com/'>cnn</a>"
);

$html = str_replace($replace_urls, $replace_with, $html);

echo $html;

?>


array_flip() would probably be useful in such a script to avoid two arrays.
Back to top View user's profile Send private message
RTAdams89
-


Joined: 06 Nov 2005
Posts: 102

PostPosted: Sat Mar 03, 2007 11:36 pm    Post subject: Reply with quote

That works perfectly. Is there anyway to make it grab the contents of the file it is embedded in though? I would rather this not have to have two separate files.
Back to top View user's profile Send private message
pkSML
-


Joined: 29 May 2006
Posts: 955
Location: Michigan, USA

PostPosted: Fri Mar 09, 2007 2:04 am    Post subject: Reply with quote

RTAdams89 wrote:
That works perfectly. Is there anyway to make it grab the contents of the file it is embedded in though? I would rather this not have to have two separate files.


Yes. You could put the html in your page as a string.

Code:
$html='the file
goes here
in a
multi-line string';


Just be sure to escape the ' character. Otherwise your script will contain syntax errors.
_________________
Stephen
Need a LitlURL?


http://CodeBin.yi.org
Back to top View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> General Questions All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group