-3

Possible Duplicate:
Replace URLs in text with HTML links

I'm writing some code to convert plain-text links to HyperText links in PHP. I'm using a regular expression (of which I know almost nothing) and I'm coming up against some problems:

  1. I don't know how to conditionally include/exclude an item in the expression
  2. I don't know how to include URL parameters in my expression
  3. I don't know much about RegEx!

This is the code I've written:

function text2link($text) 
{
    $text = preg_replace
    (
        "/(ftp\.|www\.|http:\/\/|https:\/\/|)(.*)(\.)(com|net|co\.uk)/i", 
        "<a href='http://$2$3$4' target='_blank'>$1$2$3$4</a>", 
        $text
    );

    return $text;
}

This code will convert the following "styles" of hyperlinks:

http://www.google.com
www.google.com
google.com

etc., however, the links are forced to HTTP. No matter what you type in, the expression forces HTTP. Is is possible to conditionally include the necessary protocol? For example:

"<a href='[SOME IF-CONDITION GOES HERE TO DECIDE WHAT PROTOCOL TO USE]$2$3$4' target='_blank'>$1$2$3$4</a>",

This condition would be something along the lines of:

if $1 == "http, else if $1 == "https", else if $1 == "ftp", 

etc., etc.

Also I realise I'm not accounting for anywhere near enough extensions, this is just test code right now.

Edit: I should note that I'm not looking for "fool proof", unbreakable solutions requiring 200 lines. I don't mind if some obscure domains don't get picked up. I just want the most common domains and URLs to get detected and converted to clickable links.

Community
  • 1
  • 1
user1649326
  • 247
  • 1
  • 3
  • 11
  • You current regexp will not find: `google.com` – Krycke Sep 12 '12 at 18:55
  • How about you use `preg_replace_callback` and package the condition in said callback? – mario Sep 12 '12 at 18:56
  • A useful link: http://stackoverflow.com/q/1188129/681807 – My Head Hurts Sep 12 '12 at 18:56
  • I feel left out... you don't support `.ca` domains... Determining what is and what isn't a proper internet host name is very very diffcult... `www.ca` is a perfectly valid and live website, but doesn't really appear 'legit'. – Marc B Sep 12 '12 at 18:57
  • is there any reason why you are matching also for ftp but explicitly use "http" in your replacement string? – aurora Sep 12 '12 at 18:57
  • Here's other nice question to find a URL: http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url – Krycke Sep 12 '12 at 18:59
  • @Krycke in my tests, it does work for "google.com". (@)harald yes, this is just test code. I'm trying to figure out how to conditionally use the protocol that is found by the expression (that is to say, the protocol typed in as text). – user1649326 Sep 12 '12 at 19:04
  • This question is asked about 10 times a week here. A bit of searching won't kill you. – Madara's Ghost Sep 12 '12 at 19:12
  • @Truth, I've been searching for the last hour or two and saw plenty of answers on SO and other sites. I've had trouble implementing them in my code, so I asked for help. – user1649326 Sep 12 '12 at 19:14
  • My solution: http://stackoverflow.com/questions/17900004/turn-plain-text-urls-into-active-links-using-php/17900021#17900021 – Maciej A. Czyzewski Jul 28 '13 at 00:16

1 Answers1

0

You don't need to tweak your regex:

function text2link($text, $protocol="http://") 
{
    $text = preg_replace
    (
        "/(ftp\.|www\.|http:\/\/|https:\/\/|)(.*)(\.)(com|net|co\.uk)/i", 
        "<a href='$protocol$2$3$4' target='_blank'>$1$2$3$4</a>", 
        $text
    );

    return $text;
}

You can pass in an optional protocol as a second parameter, and it'll default to http:// if you don't.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • I don't think this is possible because the $text variable is formatted HTML coming from a contentEditable
    . The protocol needs to be decided based on the protocol that is typed in by the user. For example if they type in ftp.blahblahblah.com, the expression must use the .ftp protocol in the replacement string, but it needs to be figured out inside the function. I don't think its appropriate to do a string search on the entire text because it could easily return false positives. Any ideas how to use logic inside a RegEx? I could've sworn I saw some articles on it, but can't find them.
    – user1649326 Sep 12 '12 at 19:08