Forum Moderators: coopster

Message Too Old, No Replies

Outputting hyperlinks

How?

         

Patrick Taylor

2:00 am on Jun 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've built a simple PHP blog and don't know how to make URLs included within the message show as a hyperlink. Advice would be appreciated.

The input code is:

// Input code
$query = "INSERT INTO blog (first_name, last_name, message, IP_address, posted) VALUES ('$first_name', '$last_name', '$message', '$REMOTE_ADDR', NOW() )";

The output code is:

// Output code
$query = "SELECT first_name, last_name, message, DATE_FORMAT(posted, '%M %D %Y (%l:%i)') FROM blog ORDER BY posted DESC";
$result = @mysql_query ($query); // Run the query.
if ($result) { // If it ran OK, display the records.

// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo '<p>' . $row[2] . '</p><p>From ' . $row[0] . ' ' . $row[1] . ': ' . $row[3] . '</p>';
}

The relevant variable is "message", which is a piece of text within which the user might include an URL which I would like to display as a clickable link when the message is output.

httpwebwitch

2:51 am on Jun 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?php

function insertHyperlinks($txt){
$txt = preg_replace( "/(?<!<a href=\")((http¦ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\">\\0</a>", $txt );
return $txt;
}

function insertemailHyperlinks($txt){
$txt = preg_replace("/(([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}¦[0-9]{1,3})(\]?)))/i","<A HREF=\"mailto:$0\">$2</A>",$txt);
return $txt;
}
?>

Patrick Taylor

3:08 am on Jun 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, but where do I put it? I copied it just above the query and I get an output like:


This+is+a+test+message+URL%3A+www.domain.com

Or above the query in the input code (with the txt variables altered to "message") and it doesn't yet make a link...

httpwebwitch

3:12 pm on Jun 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Those are function definitions; the functions take a string as input, and output the same string with links around any URLs or mail addresses it finds.

So don't edit the function or change the variable names. The variable $txt is working within the scope of the function definition. Just use the function like a function.

If you want to STORE the messages with links pre-added, then wrap the function around $message, like this:

insertemailHyperlinks(insertHyperlinks($message))

An easier way is to make another custom function:

[code]
function linkthem($txt){
$txt=insertemailHyperlinks($txt);
$txt=insertHyperlinks($txt);
return $txt;
}
[code]

Then you can use that one function to link up both kinds:

linkthem($message)

But you know, I wouldn't recommend storing the data with your links pre-attached. It would be better to add them when the data is output. That way you're not corrupting the original messages in case you want to reuse them in another format, or if you change your mind about having hyperlinked bits.

In that case, you wrap linkthem() around the text as it's being ECHOed.

try this:

$message = "write to mememe@home.com or visit [lala.net,...] thanks!";

print($message);
print("<BR>");
print(linkthem($message));

Good luck!

Patrick Taylor

2:14 am on Jun 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After looking around the PHP site I finally managed to get a parser to make the URLs to show as links, but it was done on the input side, and I see your point about it being maybe better the other way. So I've now altered it - no input parser - and above my query I've placed the three functions you posted (insertHyperlinks, insertemailHyperlinks, and linkThem) then in my loop:


// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo '<p>' . (linkThem($row[2])) . '</p><p>From ' . $row[0] . ' ' . $row[1] . ': ' . $row[3] . '</p>';
}

Obviously it's me (and I've used several variations on a trial-and-error basis) but those URLs still aren't showing as links. Thanks for your suggestions though - I'm sure your method works... and I'll have another go.

Patrick

Patrick Taylor

2:57 am on Jun 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... a bit later...

Actually, this:


function insertHyperlinks($txt) {
$txt = preg_replace("/(?<!<a href=\")((http¦ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\">\\0</a>", $txt);
return $txt;
}

function insertemailHyperlinks($txt) {
$txt = preg_replace("/(([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}¦[0-9]{1,3})(\]?)))/i","<A HREF=\"mailto:$0\">$2</A>", $txt);
return $txt;
}

function linkThem($txt) {
$txt = insertHyperlinks($txt);
$txt = insertemailHyperlinks($txt);
return $txt;
}

$test = "write to mememe@home.com or visit [lala.net,...] thanks!";
echo ($test);
echo ("<br>");
echo (linkThem($test));

... doesn't produce a link on the last line either.

httpwebwitch

7:08 pm on Jun 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,
I fixed the REGEX patterns. Here is some code that works.


<?php
function insertHyperlinks($txt){
$pattern = "{((ftp¦http¦https¦gopher¦mailto¦news¦nntp¦telnet¦wais¦file¦prospero¦aim¦webcal):\/\/[a-zA-Z0-9~#\/\.]*)}i";
$txt = preg_replace($pattern, "<a href=\"$0\">$0</a>", $txt );
return $txt;
}
function insertemailHyperlinks($txt) {
$txt = preg_replace("/([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)¦(([\w-]+\.)+))([a-zA-Z]{2,4}¦[0-9]{1,3})(\]?)/i","<A HREF='mailto:$0'>$0</A>", $txt);
return $txt;
}
function linkThem($txt) {
$txt = insertHyperlinks($txt);
$txt = insertemailHyperlinks($txt);
return $txt;
}

$test = "write to mememe@home.com, or visit http://www.lala.net, ASAP.";

print(insertHyperlinks($test));
print('<BR>');
print(insertemailHyperlinks($test));
print('<BR>');
print(linkThem($test));
?>

Patrick Taylor

1:07 pm on Jul 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well... thanks, but for some strange reason it never works for me. I pasted your code exactly as-is, and I don't get hyperlinks.

Because I can't get the output solution correct, I'm still doing it on the input side and it's okay for an URL but not for an email address. Here's the code I've used (you may groan)...

// URL parser
$message = ereg_replace("(([^\.<>[:space:]]+\.)¦([[:alpha:]]+://))+". "[^\.<>[:space:]]+\.[^<>[:space:]]+", "<a href=\"http://\\0\" target=\"_blank\">\\0</a>", $message);
$message=ereg_replace("http://([[:alpha:]]+://)","\\1",$message);

// Email parser
$message = ereg_replace ('[_a-zA-z0-9\-]+(\.[_a-zA-z0-9\-]+)*\@' . '[_a-zA-z0-9\-]+(\.[a-zA-z]{1,3})+', "<a href=\"mailto:\\0\">\\0</a>", $message);

// Make the query - insert data into table.
$query = "INSERT INTO mytable (message) VALUES ('$message')";

An email address comes out rather like this (not pretty):

admin@mydomain.org" target="_blank">admin@mydomain.org

I suppose I have this all wrong and shouldn't be using those 2 parsers at once?