2

In most chat applications when a user inputs a hyperlink, it automatically becomes clickable in the chat window. How do I replicate this behavior using PHP/JavaScript/JQuery?

Essentially my idea of making it work is testing the chat message string for hyperlinks and encasing them in <a href> tags with themselves as the link. Is that correct, and if so, how do I do it? Also, how would I make it so that the links open in a new tab, instead of replacing my page?

EDIT This took me quite a bit of trial and error, but this PHP function should work in all realistic scenarios:

function formatTextLinks($text) {
    $words = preg_split("/[\s,]+/", $text);
    $offset = 0;

    foreach($words as $value) {
        preg_match("/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/", $value, $matches);

        $s = $matches[0];
        if(!is_string($s)) continue;
        $pos = strpos($text, $s, $offset);
        if($pos !== false) {
            $helper = "";
            if(strpos($s, "http://") === false || strpos($s, "https://") === false) $helper = "http://";
            $text = substr_replace($text, "<a href='".$helper.$s."' target='_blank'>".$s."</a>", $pos, strlen($s));
            $offset = $pos + strlen("<a href='".$helper.$s."' target='_blank'>".$s."</a>");
        }
    }

    return $text;
}
4
  • Way to broad a question for SO I am afraid. Commented Sep 30, 2014 at 2:58
  • @RiggsFolly How would I rephrase it? It seems straightforward to me; all I'm asking is how could I detect that a string contains a url (fully-qualified or not, it just needs to work in an address bar) and then make it clickable as a hyperlink. Commented Sep 30, 2014 at 3:00
  • fully-qualified: regex for http://. Or not: Unless you have a regex for .com|.org|.net|ect, probably isnt feasable Commented Sep 30, 2014 at 3:02
  • 1
    I would have the PHP generating the page create the applicable anchor when it sends out the data. The chat application does this the same way: after the user enters input it applies a heuristic to guess if it's a URL, if it looks like a URL it treats it like a URL (same way with smiley-faces and whatnot). The actual heuristic may be "loose" such as only looking for valid_public_domain/valid_path (e.g. not needing protocol portion) or "strict" in requiring a full valid URL with protocol. Commented Sep 30, 2014 at 3:09

1 Answer 1

2

You could use a php or javascript regular expression to test if it is a URL (link). If yes create a html link tag using jquery.

This is the regex pattern

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

This is the source http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149

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

1 Comment

It was difficult figuring out a function to correctly format the text when it found the matches, especially because using preg_match_all using that regex returns matches that aren't hyperlinks at all, just parts of them (i.e. just the "http://" or the "www"). I'll post my working function into my question for any future visitors.

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.