1

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');
?>
5
  • 2
    stackoverflow.com/questions/910912/… Commented Jan 25, 2018 at 5:47
  • stackoverflow.com/questions/828870/… - check this answer also. Commented Jan 25, 2018 at 5:50
  • He's trying to do it without regex. Commented Jan 25, 2018 at 6:13
  • @robt, Should I post an alternative answer which does not use preg_replace? It would be uglier though, but it would be something like you tried. Commented Jan 25, 2018 at 6:43
  • @AtaurRahman, It would be great if you did. Your preg_replace solution was very helpful, but I would just like to figure out the issue in my code. Thanks! Commented Jan 25, 2018 at 17:35

1 Answer 1

1

Simple preg_replace() works for me

<?php

$comment = 'Blah Blah http://www.google.com?a=b+c blah blah';

$comment =  preg_replace('#\b(https?://[\S]+)#', '<a href="$1">$1</a>', $comment);

print_r($comment);

Output :

Blah Blah <a href="http://www.google.com?a=b+c">http://www.google.com?a=b+c</a> blah blah

This one won't fix any broken link, but works great for generic usage.

Regex :
\b: matches word boundaries like space or start/end of string.
https?: selects string that starts with http. ? sign makes the s optional. We will start matching our url from here.
[\S]+: selects any non-whitespace string. We will stop matching our url when we meet a space.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.