Posted By

keevkilla on 07/09/10


Tagged

regex url php pregreplace interpretation


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

keevkilla
vaxxis
GreyedDesign
Criptt


Improvement of URL interpretation with regex


 / Published in: PHP
 

A source to help you interpreting URLs in plain text as a HTML link. It's an improvement of what was proposed here : http://snipplr.com/view/2371/regex-regular-expression-to-match-a-url/

  1. // Returns the text given as parameter, with contained URLs interpreted as HTML links
  2.  
  3. function link($text){
  4. return preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="$1">$1</a>', $text);
  5. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Master21 on July 30, 2010

Problem: This improperly matches the string "SearchCriteria: abcd123456@some.ch"

Posted By: keevkilla on August 2, 2010

thank you for your remark, I'll see what I can do and post a patch when found.

Posted By: keevkilla on August 3, 2010

thank you for your remark, I'll see what I can do and post a patch when found.

Posted By: tuxfede on January 25, 2011

Thank you very much. I have a problem: I need this matches the string http://www.terra.es. (look the final point at the end of the string). How can I not to include the last point? Sorry for mystakes.

Posted By: jm1248 on February 7, 2011

To: tuxfede

I tried this with the trailing period e.g. mysite.org. and it worked perfectly.

BTW keevkilla; this is a very nice piece of work - I like the whole site.

John McKean - Toronto, Canada

Posted By: jm1248 on February 8, 2011

What I wanted was a javascript snippet and keevkilla's was PHP. Backslashing the slashes satisfies JS and this snippet works (for me). However, it does capture punctuation that follows an html append.

function clickify_links($text) { return $text.replace(/((https?:\/\/)?([-\w]+.[-\w.]+)+\w(:\d+)?(\/([-\w_.]*(\?\S+)?)?)*)/gim, ""+"$1"+""); }

As a test:

function test() { $text = "your-site,net http://yoursite.com. bb dd mysite.org! ..." $text = $text+"yoursite.com .yoursite.com! https://yoursite.com: yoursite.com? " $text = $text+"yoursite.subsite.com/dir/blurb.html?name=billy-jean.

Posted By: jm1248 on February 8, 2011

Hmm - my last comment got hopelessly mangled. Please ignore it

Posted By: onigetoc on July 23, 2011

it doesnt find query link like http://www.mysite.com/?test=test/ I.E. some long link with path, query or other character http://www.mysite.com/#test

Posted By: Criptt on March 31, 2012

Great improvement but when a user imputs a string like: www.mysite.com the link will be incorrect because in the link-tag you wont have http:// before your result. Therefor the url of the user shall try to redirect towards a page into your own site. Is their a way to solve this?

Posted By: Criptt on March 31, 2012

This is a simple solution for www domains $text= pregreplace('@((https?://)?([-\w]+.[-\w.]+)+\w(:\d+)?(/([-\w/.](\?\S+)?)?))@', '$1', $text); $text= str_replace("href=\"www.","href=\"http://www.",$text);

but when the user inserts a url like: mail.mysite.com it wont work. It wont even pass the preg_replace funtion.

You need to login to post a comment.