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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert text to hyperlink

Status
Not open for further replies.
Nov 16, 2002
24
US
Anyone knows what script is like to automatically change a URL to hyperlink so that when a user types a URL in the text field, it changes into hyperlink, like this:

Thanks,
Lea
 
Basic steps...

write a regular expression which matches URLs.

something like

$pattern = /^http://[a-zA-Z0-9&.\-\/]+$/;

Then use preg_replace with a back reference to find a URL reformat it and replace it...

$formatted_url = &quot;<a href='\${1}\'>\${1}\</a>&quot;;

preg_replace($pattern, $formatted_url, $text_to_format);

-Rob
*The above code is untested, and I might have some slight typos, all the information can be found at
And please, let me know how it works for you.
 
I was kinda hoping there could be a function to do this. I won't know what user will input. somewhat string parsing may be involved.

Thank you,
Lea
 
I gave you the elements of a function, perhaps I'm a bit confused as to what other text parsing you want to do... but preg_replace will parse the text for you (using PERL style regular expressions... very nice)?

if the user enters text, you're going to need to put it into a variable for PHP to do anything right? Then send that to the lines above... here's the whole example (the changes are because I've now gone back and actually tested this code)

Code:
<?php

function add_hypers(&$text) {
  $pattern = &quot;/http:\/\/[a-zA-Z.&\-\/]+[a-zA-Z&\-\/]+/&quot;;
  $formatted_url = &quot;<a href='\${0}'>\${0}</a>&quot;;
  $text = preg_replace($pattern, $formatted_url, $text);
  echo $text;
}

$input = &quot;Hello I have all sorts of things for you to see at [URL unfurl="true"]http://www.google.com.[/URL] please enjoy&quot;;

add_hypers($input);
echo &quot;<BR>&quot;;
echo $input;
?>
 
Thank you so much for the reply, the function works great except it doesn't include symbols. If i enter it only convert to hyperlink. I'm a newbie to PHP and none of the books i got discuss this in detail. I greatly appreciate your help and if you can also suggest some good books or references, that will be great.
 
asunpraise You just need to add the symbols I forgot into the regular expression...

$pattern = &quot;/http:\/\/[a-zA-Z.&\-\/]+[a-zA-Z&\-\/]+/&quot;;

would now be

$pattern =&quot;/http:\/\/[a-zA-Z.&\-\/~]+[a-zA-Z&\-\/~]+/&quot;;

If there are any other symbols that need to go in there, just keep putting them after the ~ one at a time, a few symbols (like the - and /) need to be &quot;escaped&quot; with the \ character, so if you suddenly start generating an error, put the \ before your symbol.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top