I've worked on this on and off for a couple of months now, and, while I have gotten it to work 90% of the time, I think it's time to admit this project's to much for my limited abilities.
Anyway, what I'm trying to do is write a script that will convert any web url it comes across in a user's comment into a hyperlink. For example, if the url http://www.phpbuiler.com was found in a comment, the script would convert it http://www.phpbuilder.com into <a href=http://www.phpbuilder.com>http://www.phpbuilder.com</a>.
The code below works most of the time, but every know and then it runs into something(still haven't figured out what) that causes the script to time out. It's probably not the most efficient way to go about this either.... If anyone has any ideas or snippets of code to share I would be very grateful.
function print_comment($comment)
{
$whitespace = " ";
$whitespace .= $comment;
$comment = $whitespace;
$i = -1;
$urlpresent = "";
$urlcheck = "";
$comment .= " ";
$comment = ereg_replace("<br>","\n",$comment);
$comlength = strlen("$comment");
$urlpresent = ereg_replace("http://","urlisfoundheremofo",$comment);
{
$urlpresent == $comment;
}
if ($urlpresent != $comment)
{
$subcomment = $comment;
while ($urlcheck != "complete")
{
$toadd = 0;
++$i;
$startoflink[$i] = strpos($subcomment," http://");
++$startoflink[$i];
$spacecheck[$i] = $startoflink[$i];
$nextcheck = $spacecheck[$i] + 7;
$subcomlength = strlen("$subcomment");
$toadd = $comlength - $subcomlength;
$spacecheck[$i] = $spacecheck[$i] + $toadd;
$newcomlength = $comlength - $nextcheck;
$subcomment = substr($subcomment,$nextcheck, $newcomlength);
$urlpresent2 = ereg_replace("http://","urlisfoundheremofo",$subcomment);
if ($urlpresent2 == $subcomment)
{
$urlcheck = "complete";
}
}
}
$x = 0;
$maxi = $i;
while ($i > -1)
{
$checker = "";
while ($checker != " " and $checker != "\n")
{
$checker = substr($comment, $spacecheck[$i], 1);
if ($checker != " " and $checker != "\n")
{
$string[$i] .= $checker;
}
++$spacecheck[$i];
}
--$i;
}
$i = $maxi;
while ($i > -1)
{
while(!ereg(".*([[:alnum:]]|/)$", $string[$i]))
{
$length = strlen("$string[$i]");
$toget = $length - 1;
$string[$i] = substr($string[$i], 0, $toget);
}
--$i;
}
sort($string);
$i = $maxi;
while ($i > -1)
{
$comment = ereg_replace(" $string[$i]"," <a href=$string[$i]>$string[$i]</a>",$comment);
--$i;
}
$comment = ereg_replace("\n","<br>",$comment);
print("$comment");
}