I need some help with urlencode in combination with eregi_replace. I have a section of text which may contain links, so I would like to make them clickable. This part I have a solution for, but now I would like to make the link go to a redirect script go.php and pass the link url via GET.
So essentially, if the text contains a link like:
http://www.othersite.com/index.php?id=8522&n=10
I would like to replace that with like:
<a href="http://www.site.com/go.php?forumlink=1&url=http://www.othersite.com/index.php?id=8522&n=10" target="_blank">Link</a>
But then the url parameter needs to be urlencoded. How do I do that with my current code? (or in some other better way. But I've had bad experiences with bugs in clickable url-scripts, so I rather modify my code if possible. thank you)
Current code, which produces desired result, except that url parameter is not urlencoded.
<?php
function makeClickableLinks($text)
{
$base_url = "http://www.site.com/go.php?forumlink=1&url=";
$text = eregi_replace('((http://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="'.$base_url.'\\1'.'" target="_blank">Link</a>', $text); //test2
return $text;
}
$text = 'Here is my link <br />http://www.othersite.com/index.php?id=8522&n=10<br /><br />and here is some other stuff.';
$newtext = makeClickableLinks($text);
echo "newtext: |".$newtext."|";
?>
Edit: Just to clarify, it´s obviously this part that needs to be urlencoded since it may contain ? and & and other strange chars:
http://www.othersite.com/index.php?id=8522&n=10