Forum Moderators: coopster

Message Too Old, No Replies

URL to Link

         

Ackergaul

9:36 pm on Apr 16, 2010 (gmt 0)

10+ Year Member



Hi all,

I am using preg_replace to convert urls directly to a link.

Here comes my code:
/*** make sure there is an http:// on all URLs ***/
$string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);
/*** make all URLs links ***/
$string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string);

However it converts
http://www.example.com/profile.php?ref=profile&id=10000 to
http://www.example.com/profile.php,
so it cuts the &id=10000. What do I have to change to keep that part?

Many thanks!

Readie

10:33 pm on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

function url_to_link($input) {
while(preg_match('/(?!<a target="_blank" href=")(?:http:\/\/)?(www(\.[\d\w\$\-_\.\+!\*\'\(\),%]+)+(\?[\d\w\$\-_\.\+!\*\'\(\),&=%]*)?)(?!(">)|(</a>))/is', $input, $out)) {
$repl = '<a target="_blank" href="http://' . $out[1] . '">' . $out[0] . '</a>';
$input = str_replace($out[0], $repl, $input)
}
return $input;
}

Ackergaul

10:58 pm on Apr 16, 2010 (gmt 0)

10+ Year Member



Hi Readie,

thank you for your reply but it does not work at all :/

Readie

11:07 pm on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yea, thought I'd messed the regex up a bit.

I'll have another go, give me a minute though.

Readie

11:12 pm on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, try that function instead.

function url_to_link($input) {
while(preg_match('/(?<!<a\starget="_blank"\shref=")(?:http:\/\/)?(www(\.[\d\w\$\-_\.\+!\*\'\(\),%]+)+(\?[\d\w\$\-_\.\+!\*\'\(\),&=%]*)?)(?!(">)|(</a>))/is', $input, $out)) {
$repl = '<a target="_blank" href="http://' . $out[1] . '">' . $out[0] . '</a>';
$input = str_replace($out[0], $repl, $input)
}
return $input;
}

Ackergaul

8:01 am on Apr 17, 2010 (gmt 0)

10+ Year Member



Hi, thank you for your message again.
However it cuts that part too and it does not convert to url to a link for some reason.

Best,
Ackergaul

Readie

10:22 am on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, let's try this a different way then. This function (if I've managed to do it right this time :D) will force the requirement of http:// to pick up the URL, which isn't really a bad thing.

function url_to_link($input) {
if(preg_match_all('/http:\/\/(?:www\.)?([^\.\s]+)(\.[^\.\?\s]+)+(\?.*)?/is', $input, $out)) {
foreach($out[0] as $find) {
$repl = '<a target="_blank" href="' . $find . '">[' . $out[1] . ']</a>';
$input = str_replace($find, $repl, $input);
}
}
}

Readie

3:39 pm on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



http:\/\/(?:www\.)?([^\.\s]+)(\.[^\.\?\s]+)+(\?.*)?

Might want to do

https?:\/\/
etc...

to allow for http:// or https://

Ackergaul

12:00 am on Apr 19, 2010 (gmt 0)

10+ Year Member



Same problem :/

Readie

8:56 am on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Made a bit of a stupid mistake with the function I wrote :/ no return, and I've messed up the calls to the array:

function url_to_link($input) {
if(preg_match_all('/http:\/\/(?:www\.)?(([^\.\s]+)(\.[^\.\?\s]+)+)(\?.*)?/is', $input, $out)) {
foreach($out as $find) {
$repl = '[<a target="_blank" href="' . $find[0] . '">' . $find[1] . '...</a>]';
$input = str_replace($find[0], $repl, $input);
}
}
return $input;
}
url_to_link($my_unformatted_string);