Link in tolls

0

I'm getting texts from my database, I wanted to know, if there is any link inside this text, how do I make it appear as a normal link to everyone?

    
asked by anonymous 26.05.2018 / 00:45

1 answer

2

Basically you will need to do a replace

Regular expression

~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~

Implementation in PHP

$texto = "Oi, sou Mauro, costumo acessar vários sites como http://www.facebook.com";

$texto = preg_replace(
    "~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~", 
    "<a href=\"\0\">\0</a>", $texto
);

echo $texto;

See it working: link

Libraries

If you do not want to reinvent the wheel, there are some libs that do this work for you, these are the ones I use / recommend and recommend.

misd-service-development / php-linkify

Jasny - Gist

    
26.05.2018 / 00:57