Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to make links clickable in block of text [duplicate]

Tags:

I want:

Here is link: http://google.com And http://example.com inside. And another one at the very end: http://test.net 

to become:

Here is link: <a href="http://google.com">http://google.com</a> And <a href="http://example.com">http://example.com</a> inside. And another one at the very end: <a href="http://test.net">http://test.net</a> 

Seems like a trivial task, but I cannot find a PHP function that works. Do you have any ideas?

function make_links_clickable($text){     // ??? }  $text = 'Here is link: http://google.com And http://example.com inside. And another one at the very end: http://test.net';  echo make_links_clickable($text); 
like image 430
Silver Light Avatar asked Mar 17 '11 15:03

Silver Light


People also ask

How do you make a link clickable in text?

Right-click anywhere on the link and, on the shortcut menu, click Edit Hyperlink. In the Edit Hyperlink dialog, select the text in the Text to display box. Type the text you want to use for the link, and then click OK.

How do I make a div link clickable?

We simply add the onlcick event and add a location to it. Then, additionally and optionally, we add a cursor: pointer to indicate to the user the div is clickable. This will make the whole div clickable.

What is Block link?

Link blocks are similar to Div blocks in that they can be used for structure and layout, but the area inside of the Link block becomes a link. Link blocks can turn any element, like an image, or any layout, like a banner, into a link.


2 Answers

Use this (works with ftp, http, ftps and https schemes):

function make_links_clickable($text){     return preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1">$1</a>', $text); } 
like image 54
Akarun Avatar answered Sep 29 '22 12:09

Akarun


Try something like this:

function make_links_clickable($text) {    return preg_replace ('/http:\/\/[^\s]+/i', "<a href=\"${0}\">${0}</a>", $text); }   $result = make_links_clickable($text); 
like image 23
Marco Demaio Avatar answered Sep 29 '22 13:09

Marco Demaio