Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto Hyperlinking to Words & Phrases

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
On a PHP/MySQL site where all content is in the database, I have been hard-coding crossreference links but it occurs to me that this can probably be done automatically.

For example, if the content has "Joe Blow" (with or without the quotes) and no existing hyperlink, it would link to: /php/pagecontents.php?Keyword=Joe+Blow if Joe Blow is also in the list.

Given a table containing a list of terms to find and given a preset URL using the same term as a paramater to pass as in the example above, how can I begin? I know PHP fairly well but am not sure where to start!

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Easily you can use php's preg replace function.

First assign all your keywords to an array.

Code:
$keywords = array("key1","key2");

Now you would have to make the strings in regex form, you can easily do this by creating a function and using array_map.

Code:
$keywords = array_map('regexForm', $keywords);

function regexForm($string)
{
   return '/('.$string.')/i';
}

That will make your array look like this

Code:
'/(key1)/i','/(key2)/i';

Next you would have to run the replace, say $content contains what your searching in.

Code:
$newContent = preg_replace($keywords,'<a href="/php/pagecontents.php?Keyword=\1">\1</a>',$content);

So your finally code would be something like this.

Code:
$content = 'My content key1 here';
$keywords = array("key1","key2");
$keywords = array_map('regexForm', $keywords);

$newContent = preg_replace($keywords,'<a href="/php/pagecontents.php?Keyword=\1">\1</a>',$content);

print $newContent;

function regexForm($string)
{
   return '/('.$string.')/i';
}

Now I am not positive it works because I can't test it, but that's how you would do it :)

Andrew
 
Thanks for the ideas! The keywords/phrases already exist in a MySQL table and are recording titles - several hundred of them - so I'll need to query this table somehow in order to do it. May be too many for an array but I can still loop through them and use your other ideas to insert the links.

Ultimately, I would like to do it as a function where it can be plugged into the form that adds/edits the pages so that it can actually place the link code directly into the page text. However, for pages that haven't been added or edited recently, I also want to use the function to do it on-the-fly as the page is loaded by the site visitor.

You have given some very valuable tips and I'll post again once I get it working to say how I ended up doing it. In the meantime, I am open to any ideas, particularly in the area of making it not create links where they already exist! Thanks again.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
This is working now using the existing table of recording titles by looping through them to find matches, then adding the link.

However, I don't know regular expressions enough to know how to make it not link if already linked or, better yet, to replace the link using the newly generated one.

Here's what I have but note that there are some custom fuctions in this code that may not be known by most but they probably explain themselves well enough for the example:

Code:
function autoLink($BodyText) {

$DBconn = new clsDBconn();
        $DBconn->query("SELECT Title FROM recordtitles");
        while ($DBconn->next_record()) {
                $RecordTitle = $DBconn->f("Title");
                $BodyText = str_replace($RecordTitle, "<a href=\"/php/recordings.php?Title=" . urlencode(stripslashes($RecordTitle)) . "\">" . $RecordTitle . "</a>", $BodyText);
        }
}

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top