Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert plain text URLs into HTML hyperlinks in PHP

I have a simple commenting system where people can submit hyperlinks inside the plain text field. When I display these records back from the database and into the web page, what RegExp in PHP can I use to convert these links into HTML-type anchor links?

I don't want the algorithm to do this with any other kind of link, just http and https.

like image 921
Volomike Avatar asked Dec 25 '09 04:12

Volomike


People also ask

How do I convert plain text to hyperlink?

If you just want to format existing text into a hyperlink: Select the text that you want to turn into a hyperlink, and right-click it. On the shortcut menu, click Hyperlink. In the Insert Hyperlink dialog, paste the link in the Address box and click OK.

How can we create links in PHP pages?

PHP link() Function$linkname = "mylink"; link($target, $linkname);

What is a plain text URL?

If you send your email in plain text, then your URL will be plain text. It's plain text, you can't dress it up. If you send it as HTML, just use a simple anchor tag and use the URL as both the href and the text. That way if a mail client removes the link at least the user will still be able to copy/paste the url.


2 Answers

Here is another solution, This will catch all http/https/www and convert to clickable links.

$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';  $string = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $string); echo $string; 

Alternatively for just catching http/https then use the code below.

$url = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';    $string= preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $string); echo $string; 

EDIT: The script below will catch all URL types and convert them to clickable links.

$url = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@'; $string = preg_replace($url, '<a href="http$2://$4" target="_blank" title="$0">$0</a>', $string); echo $string; 

The new update, If you're having the string strip the (s) then use the below code block, Thanks to @AndrewEllis for pointing this out.

$url = '@(http(s)?)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@'; $string = preg_replace($url, '<a href="http$2://$4" target="_blank" title="$0">$0</a>', $string); echo $string; 

Here's a very simple solution for the URL not displaying correctly.

$email = '<a href="mailto:[email protected]">[email protected]</a>'; $string = $email; echo $string; 

It is a very simple fix but you will have to modify it for your own purpose.

I've provided multiple answers as some servers are set up differently, so one answer may work for some but not for others, but I hope the answer(s) work for you and if not then let me know, and hopefully, I can come up with another solution.

There are multiple scripts as some PHP files require different scripts also some servers are set up differently, Plus each has different requirements, Some want just HTTP/S, some want WWW and some want FTP/S, Each one will work depending on how the users own scripts are set up, I provided some text with each one with what they do.

like image 68
Rudder Avatar answered Oct 16 '22 18:10

Rudder


Well, Volomike's answer is much closer. And to push it a bit further, here's what I did for it to disregard the trailing period at the end of the hyperlinks. I also considered URI fragments.

public static function makeClickableLinks($s) {   return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s); } 
like image 43
MkVal Avatar answered Oct 16 '22 19:10

MkVal