Finding urls and making them hyperlinks

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I want to take a block of text and add html tags to make hyperlinks
where hyperlink strings exist.  So, if I have a variable

$a = "This is "

I would like to run that through a function such that the value of $a

"This is <a href=' '> </a>"

How can I do this?  Any help is greatly appreciated.  I am using PHP 4.

Thanks, - Dave

Re: Finding urls and making them hyperlinks

On 19 Nov 2005 13:39:46 -0800, wrote:
Quoted text here. Click to load it

making the following 2 assumptions:

URL begins with " http" (and there is only one instance within $a)
URL contains no spaces

something like the following (untested) code should work - (this can
most likely be optimized and I'd be interested to know how if anyone
can help out):


$a = "This is ";

// break everything to the left of "http"
$MyURL = substr($a, strpos($a, "http"));
$MyIntro = substr($a, 0, strpos($a, "http"));

// remove anything after URL (if existing)
if (strpos($MyURL," ")) {   // there is a space in the string (after
    $MyURL = substr($MyURL, 0, strpos($MyURL, " "));

$MyOutput = $MyIntro . "<a href=\"" . $MyURL . "\">" . $MyURL .

echo $MyOutput;

John McClumpha
Darkness Reigns -

Re: Finding urls and making them hyperlinks

Something like this should do the trick:

$a = preg_replace('/(http:\S+)/', '<a href=""></a>', $a);

Site Timeline