Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phrane's avatar

How do I detect and display url links from standard text?

I'm currently following L6-from-Scratch series, in particular the Tweet app. The app uses standard html form for text input, then displays this further down the page (like Twitter).

Question... If I "tweet" some text which also contains a url, how can I display and make active and url link contained within it?

TIA for help on this,

P

0 likes
4 replies
Obayda's avatar

you can use this function to do what you want wit JS

function linkify(text) { var urlRegex =/(\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~|!:,.;]*[-A-Z0-9+&@#/%=~|])/ig; return text.replace(urlRegex, function(url) { return '' + url + ''; }); } or with PHP you can see this link https://gist.github.com/jasny/2000705

1 like
phrane's avatar

Obayda, Tippin, thankyou guys. I didn't even know where to begin!! Cheers, P

1 like
phrane's avatar

Hey T, I was looking further into Autolinker, and have imported the js into my project. The part I want autolinker working on is a string table output (named 'body') which is being output from a blade template (below):

{{ $tweet->body }}

I'm guessing I just need to apply the autolinker to this "body" property, but i've got no idea how to do it.

Any ideas? Cheers

Please or to participate in this conversation.