Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto hyperlink urls

Status
Not open for further replies.

oohoohoohooh

Programmer
Apr 30, 2005
55
GB
Hi, I want to create a function that will scan through a variable and convert any urls to auto hyperlink. I would appreciate if someone could show me how I could do this. Also could the function truncate the initial link display such that it would say ... (after so many characters) to avoid page stretching issues. Thanks
 
can i have an example???

Known is handfull, Unknown is worldfull
 
You would use regular expressions to do that.
I remember several threads in this forum about that - try the search function and if you don't find anything, let us know.
 
Hi I tried searching this forum but with no luck. The kind of variable I would need to carry this out on is a typical forum post. I imagine it would look for http:// and carry out the link from there to the next space (minus the full stop, bracket etc if there is one at the end).
 
can i have an example string like:
$str="ASDASDasd";


and what it should look like after being replaced???

Known is handfull, Unknown is worldfull
 
You will have to have a look at regular expressions. We can help witht hat.
However, as vbkris points out, please give us an example of what the strings would look like.
Are you looking to convert and or whatever.org
All these can be captured using regular expressions and surrounded by anchor tags.

Also look here: thread434-1005843
 
Ah right yeah well i would be looking to convert the following string (as an example):

"Hi welcome to
to

"Hi welcome to <a href="
Basically it hyperlinks any address you specify in the string. The ... at the end is displayed if it goes over say 15 characters. This is to avoid the page stretching if the url is too long.

Hope that makes things clearer. Cheers
 
<?
$TheStr="Hi welcome to asd.";
$TheStr=preg_replace("/(http:\/\/[-a-z0-9\._]+)/","<a href='$1'>$1</a>",$TheStr);
echo($TheStr);
?>

Known is handfull, Unknown is worldfull
 
Hi cheers, I tried $TheStr="Hi welcome to asd."; but the full stop at the end was also hyperlinked. Any chance you could change it cuts off before the fullstop. Cheers
 
try this:
$TheStr=preg_replace("/(http:\/\/(?:[-a-z0-9_]+\.)+[a-z]+)/","<a href='$1'>$1</a>",$TheStr);


Known is handfull, Unknown is worldfull
 
Actually I've just found a problem. If you have the url the index.htm part does not get hyperlinked. Here's my modified version after trying to understand what it all does:

$output = preg_replace('/((http)+(s)?:\/\/(?:[-A-Za-z\d_]+\.)+[a-z]+)/', '<a href="\\1" target="_blank">\\1</a>', $output);
 
Hi, after further worked I've come up with:

$output = "Hi welcome to ftps:// asd.";
$output = preg_replace('/(http|ftp)(s)?:\/\/([-A-Za-z\d_.]+)+([a-z]+)([-A-Za-z\d_\/.?&=]+)?/', '<a href="\\0" target="_blank">\\0</a>', $output);

and returns

Hi welcome to <a href="ftps:// target="_blank">ftps:// asd.

But again the fullstop is hyperlinked. I'm not sure how to overcome this because I need the last one to have a fullstop to specify the file extension. If there's an easier I'd appreciate if you could show me. Thanks
 
new pattern:
/((http|ftps?):\/\/(?:[-a-z0-9_\/]+\.)+[a-z]+)/

please note that i have excluded querystring values (as we dont know what characters can come there)...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top