View previous topic :: View next topic |
Author |
Message |
RTAdams89 -
Joined: 06 Nov 2005 Posts: 102
|
Posted: Fri Mar 02, 2007 4:16 am Post subject: Dynamically adding hyperlinks |
|
|
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 |
|
|
abyssisthebest -
Joined: 30 Jun 2005 Posts: 319 Location: Boston, UK
|
Posted: Fri Mar 02, 2007 8:21 pm Post subject: |
|
|
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 |
|
|
cmxflash -
Joined: 11 Dec 2004 Posts: 872
|
Posted: Fri Mar 02, 2007 8:39 pm Post subject: |
|
|
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 |
|
|
RTAdams89 -
Joined: 06 Nov 2005 Posts: 102
|
Posted: Fri Mar 02, 2007 9:23 pm Post subject: |
|
|
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 |
|
|
abyssisthebest -
Joined: 30 Jun 2005 Posts: 319 Location: Boston, UK
|
Posted: Sat Mar 03, 2007 11:41 am Post subject: |
|
|
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 |
|
|
cmxflash -
Joined: 11 Dec 2004 Posts: 872
|
Posted: Sat Mar 03, 2007 1:07 pm Post subject: |
|
|
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 |
|
|
RTAdams89 -
Joined: 06 Nov 2005 Posts: 102
|
Posted: Sat Mar 03, 2007 11:36 pm Post subject: |
|
|
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 |
|
|
pkSML -
Joined: 29 May 2006 Posts: 955 Location: Michigan, USA
|
Posted: Fri Mar 09, 2007 2:04 am Post subject: |
|
|
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 |
|
|
|