Jump to content

Auto Link Generator help


anthropos9

Recommended Posts

Hi,

 

I want to be able to make a script that identifies a url that is in plain text and automatically make it into a link.  It has to be able to identify a url that begins with either http:// or just www.  I've looked all over for a script like this  - and I know that I've seen one somewhere, but I can't seem to find one now.  I'd make one myself, but I don't know where to start - so that kind of advice would also be welcome.  Thanks for your help.

Link to comment
Share on other sites

the regex validation is good, but it needs to identify the url from within a paragraph without any user input telling it $x is the url that you need to make into a link.  for instance, in the paragraph:

You can find more information www.somesite.com, because that's the only place they actually have any information on this insane topic.

the www.somesite.com should automatically be found and made into the html

<a href="http://www.somesite.com">www.somesite.com</a>

(Just like what happens on this site, since in the post it was turned into a link on its own.)

Link to comment
Share on other sites

marten_berglund at hotmail dot com

18-Apr-2007 11:20

I couldn't find any url2html-solution here that met all these three criteria:

 

1) Make links of standard web-urls (http://some.com), urls without http (some.com) and emails

2) Urls from all possible protocols (not just http and ftp)

3) To not link delimiter characters surrounding a link, e.g. "My site (www.site.com) is good!"

 

The following code takes care of all this. It is easy to below add extra characters that you don't want in the beginning/end of a link, and to add extra abbreviations that should not be linked (below adopted for some common english ones). By disallowing digits in the end of an url, time stamps like 04.57 won't be linked.

 

It seems to work fine with almost any imaginable url but please feel free to make corrections.

 

<?php
function linkify($text) {
    $strip_lchrs = "[^\s\(\)\[\]\{\}\.\,\;\:\?\!]";      //Not these chars in beginning
    $strip_rchrs = "[^\s\(\)\[\]\{\}\.\,\;\:\?\!\d]";    //Not these chars in end

    $prot_pat = $strip_lchrs . "[\S]*\:\/\/[\S]*\.[\S]*" . $strip_rchrs;  //abc://de.fg
    $email_pat = $strip_lchrs . "[\S]*@[\S]*\.[\S]*" . $strip_rchrs;      //abc@de.fg
    $general_pat = $strip_lchrs . "[\S]*\.[\S]*" . $strip_rchrs;          //abc.de

    $preg_pattern = "/(" . $prot_pat . "|" . $email_pat . "|" . $general_pat . ")/ei";
    $preg_replace = "check_preg('$1')";
    return preg_replace($preg_pattern, $preg_replace, $text);
}

function check_preg($subpat) {
    //These abbr. should not be linked
    $exc_string = "/e\.g|i\.e|et\.c/i";
    //If it says abc://de.fg
    if (preg_match("/[\S]*\:\/\/[\S]*\.[\S]*/i", $subpat) == 1) {
        return "<a href='" . $subpat . "' target='_top'>" . $subpat . "</a>";
    }
    //If it says abc@de.fg
    else if (preg_match("/[\S]*@[\S]*\.[\S]*/i", $subpat) == 1) {
        return "<a href='mailto:" . $subpat . "'>" . $subpat . "</a>";
    }
    //If it says "e.g." don't link
    else if (preg_match($exc_string, $subpat) == 1) {
        return $subpat;
    }
    //If it says abc.de
    else
        return "<a href='http://" . $subpat . "' target='_top'>" . $subpat . "</a>";
}
?>

 

Obviously you were not looking in the right spot. Came straight from the php.net manual off of the preg_replace users comment section.

 

I have not tested, but I am sure it works great.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.