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');
?>
robt
  • 67
  • 7
  • 2
    https://stackoverflow.com/questions/910912/extract-urls-from-text-in-php – Eddie Jan 25 '18 at 05:47
  • https://stackoverflow.com/questions/828870/php-regex-how-to-get-the-string-value-of-html-tag - check this answer also. – Priyank Jan 25 '18 at 05:50
  • He's trying to do it without regex. – Taha Paksu Jan 25 '18 at 06: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. – Ataur Rahman Jan 25 '18 at 06: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! – robt Jan 25 '18 at 17:35

1 Answers1

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.

Ataur Rahman
  • 1,671
  • 14
  • 12