I've studied the posts on this before but They all seem to assume the user enters the "http://" before the www. I want to enter it for them. Below is the code I've come up with.
// function takes the users message as argument
function addlinks($mess)
{
// appends a space to the end of the message
$mess.=" ";
//finds first url beginning with "www."
$start=strpos($mess,"www.",0);
while($start) //or while start !=false
{
//length of the message
$len=strlen($mess);
//finds first space after the "www."
$end=strpos($mess," ",$start);
//assigns the text between the "www." and the next space to $link
$link=substr($mess,$start,($end-$start));
//replaces the $link text with an a href link
$mess=str_replace($link,"<a href=http://$link>$link</a>",$mess);
// tries to find the next "www." after $end
//the problem is here i believe
$start=strpos($mess,"www.",$end);
return $mess;
}
The problem is the $start is never false. It gives $start a value larger than the length of the message. Any ideas on what I'm doing wrong?