Forum Moderators: coopster
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.
<?phpfunction 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;
}
?>
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!
// 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
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.
<?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));
?>
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?