Forum Moderators: coopster

Message Too Old, No Replies

Preg Replace and URLs

Problems with my preg_replace for URLS

         

mankey

11:51 am on Aug 1, 2005 (gmt 0)

10+ Year Member



Hi, I'm having some problems with my URL-code, which automatically turns adressess into urls.

Problems is that when people type "something.something else" that's also turned into an url.

So what I'd like to do is change the below expression to only turn it into urls when you type either "www" in front (www.google.com) or when you use "http://" (http://google.com,http://www.google.com).


$text_s = preg_replace("/(?<=^¦\s)((https?¦ftps?):\/\/)?(([-_a-zA-Z0-9]+\.)+)([a-zA-Z0-9]{2,6})(\/[^\]\[\s]*)?(?=\s¦$)/e","'$1'!=''? '<a href=\"$1$3$5$6\" target=\"_blank\">$1$3$5$6</a>':'<a href=\"http://$3$5$6\" target=\"_blank\">$3$5$6</a>'",$text_s);

Thanks in advance =)

mcibor

12:50 pm on Aug 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Taken from www.php.net/ereg_replace

If you want the function to process query strings, such as:

[php.net...]

modify the function as follows:

function hyperlink(&$text)
{
// match protocol://address/path/
$text = ereg_replace("[a-zA-Z]+://([-]*[.]?[a-zA-Z0-9_/-?&%])*", "<a href=\"\\0\">\\0</a>", $text);

// match www.something
$text = ereg_replace("(^¦ )(www([-]*[.]?[a-zA-Z0-9_/-?&%])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text);
}