Link in tolls

Asked

Viewed 24 times

0

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

  • There are two questions: "I wanted to know, if there is a link inside this text" Can’t you see? Second: "how do I make it appear as a normal link" To know if there is a link. Just view the text. The link within an X text can come in the following ways: http://www or <a href="http://". Would that be more or less what you want? Are there html tags? If yes, edit your question and enter the text/code blocks.

1 answer

2

Basically you will need to 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 in operation: http://sandbox.onlinephpfunctions.com/code/15e9d2a36e35ef396e4fd54fbdb4efaffa68890d

Libraries

If you don’t want to reinvent the wheel, there are some libs who do this work for you, these are the ones I use/used and recommend.

misd-service-Development/php-linkify

Jasny - Gist

Browser other questions tagged

You are not signed in. Login or sign up in order to post.