I am adding a user comment feature to a webpage. The html file has a h1 tag and everything else in the body is user comments. The idea is the check for the string "http" in the comment, find the whole url that the user has typed in, and surround it with <a href="
and "></a>
so that it is a clickable link. I have found a couple of ways to do this using regular expressions, but I am trying to take a different route and using the php library functions. The code snippet below shows what I have so far. When the html page loads and I add comments without urls or "http" the comments show up fine. However, when I add an url it only displays whatever comes before "http." I think the problem starts when I separate the user comment into $beforeLink
and $afterLink
but I am having trouble finding exactly where the issue is.
<?php
$username = $_POST["username"];
$usercomment = $_POST["usercomment"];
$c = file_get_contents("addComment.html");
$arraystring = explode("</h1>", $c);
$beforeLink=stristr($usercomment, 'http', true);
$linkStart=stristr($usercomment, 'http');
if($linkStart !== false){
$text = explode($linkStart, " ");
$link = $text[0];
$text[0] = '<a href="' . $link . '">' . $link . '</a>';
$fullText = implode($text, " ");
$usercomment=$beforeLink . " " . $fullText;
}
$result = $arraystring[0]. "</h1>". "<div class='commentbox'><p class='usercomment'>". $usercomment . "</p><div class= 'username'>" . $username . "</div><div class='date'>" . $date . "</div></div>".$arraystring[1];
file_put_contents("addComment.html",$result);
header('Location:addComment.html');
?>