Home  >  Article  >  Web Front-end  >  JavaScript and PHP custom functions that convert URL addresses in text into clickable links_javascript skills

JavaScript and PHP custom functions that convert URL addresses in text into clickable links_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:40:511934browse

When writing a small program these days, I need to use regular expressions to match the URL address in the text input by the user, and then replace the URL address with a clickable link. URL address matching, I think this should be something that everyone often uses in verification processing. Here is a relatively complete expression that I integrated:

Copy code The code is as follows:

var URL = /(https?://|ftps?://)?((d{1,3}.d{1,3}.d{1,3}.d{1,3})(: [0-9] )?|(localhost)(:[0-9] )?|([w] .)(S )(w{2,4})(:[0-9] )?)(/ ?([w#!:.? =&%@!-/] ))?/ig;

This expression can match URL addresses of http, https, ftp, ftps and IP addresses. It is still a relatively complete URL address matching calculation. Using this expression, I wrote two small functions to replace the URL address of the user's message with a clickable link. There is nothing too difficult. Just use JavaScript's replace() function to replace the URL with link:

JavaScript version:

Copy code The code is as follows:

/**
*JavaScript version
* Convert URL address into complete A tag link code
​*/
var replaceURLToLink = function (text) {
text = text.replace(URL, function (url) {
            var urlText = url;
If (!url.match('^https?://')) {
                url = 'http://' url;
            }
                return '' urlText '';
        });

return text;
};

PHP version:

Copy code The code is as follows:

/**
* PHP version modified based on Silva code
* Convert URL address into complete A tag link code
​*/
/**=============================================
 NAME        : replace_URLtolink()
 VERSION     : 1.0
 AUTHOR      : J de Silva
 DESCRIPTION : returns VOID; handles converting
 URLs into clickable links off a string.
 TYPE        : functions
 =============================================*/

function replace_URLtolink($text) {
    // grab anything that looks like a URL...
    $urls = array();
   
    // build the patterns
    $scheme = '(https?://|ftps?://)?';
    $www = '([w] .)';
    $local = 'localhost';
    $ip = '(d{1,3}.d{1,3}.d{1,3}.d{1,3})';
    $name = '([w0-9] )';
    $tld = '(w{2,4})';
    $port = '(:[0-9] )?';
    $the_rest = '(/?([w#!:.? =&%@!-/] ))?';
    $pattern = $scheme.'('.$ip.$port.'|'.$www.$name.$tld.$port.'|'.$local.$port.')'.$the_rest;
    $pattern = '/'.$pattern.'/is';
   
    // Get the URLs
    $c = preg_match_all($pattern, $text, $m);
   
    if ($c) {
        $urls = $m[0];
    }
   
    // Replace all the URLs
    if (! empty($urls)) {
        foreach ($urls as $url) {
            $pos = strpos('http://', $url);
           
            if (($pos && $pos != 0) || !$pos) {
                $fullurl = 'http://'.$url;
            } else {
                $fullurl = $url;
            }
           
            $link = ''.$url.'';
           
            $text = str_replace($url, $link, $text);
        }
    }
   
    return $text;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn