Page 1 of 1

Links submitted through forms...

Posted: Sun Feb 06, 2005 7:34 pm
by s.dot
I have this code:

Code: Select all

$smilies = array(too many smilies to post in this message);
$smiliesimg = array(too many images to post);
$message2 = str_replace($smilies, $smiliesimg, $message);
$message3 = mysql_real_escape_string(nl2br(strip_tags($message 2, '<img><a>')));
$pattern = "^(http|https|ftp)\://&#1111;a-zA-Z0-9\-\.]+\.&#1111;a-zA-Z]&#123;2,3&#125;(:&#1111;a-zA-Z0-9]*)?/?(&#1111;a-zA-Z0-9\-\._\?\,''/\\\+&%\$#\=~])*&#1111;^\.\,\)\(\s]$";
$replacement = "<a href="\\1">\\1</a>";
$message4 = eregi_replace($pattern, $replacement, $message3);
$sql = "INSERT INTO privatemessages (message, whosent, whoreceived, date, time, previousmessage) VALUES ('$message4', '".$_COOKIE&#1111;'username']."', '$musername', '$date', '$time', '$previousmessage')";
$query = mysql_query($sql) or die(mysql_error());
Here's the problem:

When I type in a link before I put in any text I get a link that says http:// but the rest of the link isn't there. If I type in text before I put a link it won't catch the link at all, and it just shows up as regular text.

Also, it only catches links that start with http://, https://, and ftp://. It won't catch links that start with www (as most people would input when submitting a link).

Any help?

(PS, in lamons terms I'm trying to automatically turn links into hyperlinks when submitted through a form)


feyd | please use the formatting tags we provide

Posted: Sun Feb 06, 2005 7:41 pm
by feyd
your regular expression is being asked to remember \\1 as (http|https|ftp).. not the entire string it finds.

As for it not finding it when after text, ^ and $ are begin and end of line markers.

...

Posted: Sun Feb 06, 2005 7:52 pm
by s.dot
Well I got that regular expression from http://www.regexlib.com . Simply because I'm not very good with writing them myself. How would I modify this expression to include the whole link instead of just the http:// part?

Posted: Sun Feb 06, 2005 8:06 pm
by feyd
This is the function used by phpbb to make links and things clickable.. I would strongly suggest not allowing anchor tags to be posted unless you place massive filtering on them as a person can snake in all kinds of nasty code. Same goes for image tags. At the very least, mysql_real_escape_string() should be run on the submission after the regular expression runs through the information.

Code: Select all

/**
 * Rewritten by Nathan Codding - Feb 6, 2001.
 * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
 * 	to that URL
 * - Goes through the given string, and replaces www.xxxx.yyyy&#1111;zzzz] with an HTML <a> tag linking
 * 	to http://www.xxxx.yyyy&#1111;/zzzz]
 * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
 *		to that email address
 * - Only matches these 2 patterns either after a space, or at the beginning of a line
 *
 * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe
 * have it require something like xxxx@yyyy.zzzz or such. We'll see.
 */
function make_clickable($text)
&#123;

	// pad it with a space so we can match things at the start of the 1st line.
	$ret = ' ' . $text;

	// matches an "xxxx://yyyy" URL at the start of a line, or after a space.
	// xxxx can only be alpha characters.
	// yyyy is anything up to the first space, newline, comma, double quote or <
	$ret = preg_replace("#(^|&#1111;\n ])(&#1111;\w]+?://&#1111;^ "\n\r\t<]*)#is", "\\1<a href="\\2" target="_blank">\\2</a>", $ret);

	// matches a "www|ftp.xxxx.yyyy&#1111;/zzzz]" kinda lazy URL thing
	// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
	// zzzz is optional.. will contain everything up to the first space, newline, 
	// comma, double quote or <.
	$ret = preg_replace("#(^|&#1111;\n ])((www|ftp)\.&#1111;^ "\t\n\r<]*)#is", "\\1<a href="http://\\2" target="_blank">\\2</a>", $ret);

	// matches an email@domain type address at the start of a line, or after a space.
	// Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
	$ret = preg_replace("#(^|&#1111;\n ])(&#1111;a-z0-9&\-_.]+?)@(&#1111;\w\-]+\.(&#1111;\w\-\.]+\.)*&#1111;\w]+)#i", "\\1<a href="mailto:\\2@\\3">\\2@\\3</a>", $ret);

	// Remove our padding..
	$ret = substr($ret, 1);

	return($ret);
&#125;

Posted: Sun Feb 06, 2005 8:16 pm
by s.dot
Awesome! I'm not quite sure how to incorporate this into my code, as I usually don't write functions, but just use predefined functions in PHP.

Would it be like this?

Code: Select all

<<PHPBB Linking function here>>

$message3 = mysql_real_escape_string(nl2br(strip_tags($message 2, '<img><a>'))); 
$message4 = eregi_replace($function, $message3);

Posted: Sun Feb 06, 2005 8:27 pm
by feyd
it's supposed to be called like any predefined function:

Code: Select all

$message = make_clickable($message);

Thank you

Posted: Sun Feb 06, 2005 8:51 pm
by s.dot
Thank you, this is perfect.
Sorry about not using the code function, it was my first post.

Posted: Sun Feb 06, 2005 8:52 pm
by feyd
it's perfectly fine.. we are used to needing to fix posts.. :?