Implementing Regular Expression Web Addresses

I found the following online, but I had problems with its implementation

(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

This is what I want to do php:

Take the following: Look here: http://www.rocketlanguages.com/spanish/resources/pronunciation_spanish_accents.php

And turn it into: Look here: <a href="http://www.rocketlanguages.com/spanish/resources/pronunciation_spanish_accents.php">http://www.rocketlanguages.com/span...anish_accents.php</a>

If the URL is long, then the text breaks into ... in the middle

+5
source share
2 answers

Try the following:

// URL regex from here:
// http://daringfireball.net/2010/07/improved_regex_for_matching_urls
define( 'URL_REGEX', <<<'_END'
~(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»""‘’]))~
_END
);

// PHP 5.3 or higher, can use closures (anonymous functions)
function replace_urls_with_anchor_tags( $string,
                                        $length = 50,
                                        $elision_string = '...' ) {
    $replace_function = function( $matches ) use ( $length, $elision_string) {
        $matched_url = $matches[ 0 ];
        return '<a href="' . $matched_url . '">' .
                abbreviated_url( $matched_url, $length, $elision_string )   .
                '</a>';
    };
    return preg_replace_callback(
        URL_REGEX,
        $replace_function,
        $string
    );
}

function abbreviated_url( $url, $length = 50, $elision_string = '...' ) {
    if ( strlen( $url ) <= $length ) {
        return $url;
    }
    $width_either_side = (int) ( ( $length - strlen( $elision_string ) ) / 2 );
    $left  = substr( $url, 0, $width_either_side );
    $right = substr( $url, strlen( $url ) - $width_either_side );

    return $left . $elision_string . $right;
}

(The return in the URL_REGEX definition confuses the syntax highlighting of stackoverflow.com, but this is nothing to worry about)

The function replace_urls_with_anchor_tagstakes a string and changes all the URLs matching inside, to the tag binding, shortening the long URLs with ellipses with ellipses. The function accepts optional arguments lengthand elision_stringif you want to play with the length and change the ellipses to something else.

Here is a usage example:

// Test it out
$test = <<<_END
Look here:
http://www.rocketlanguages.com/spanish/resources/pronunciation_spanish_accents.php

And here:
http://stackoverflow.com/questions/12385770/implementing-web-address-regular-expression
_END;

echo replace_urls_with_anchor_tags( $test, 50, '...' );
// OUTPUT:
// Look here:
// <a href="http://www.rocketlanguages.com/spanish/resources/pronunciation_spanish_accents.php">http://www.rocketlangua...ion_spanish_accents.php</a>
//
// And here:
// <a href="http://stackoverflow.com/questions/12385770/implementing-web-address-regular-expression">http://stackoverflow.co...ress-regular-expression</a>

: PHP 5.2 , replace_urls_with_anchor_tags, create_function . PHP 5.3:

// No closures in PHP 5.2, must use create_function()
function replace_urls_with_anchor_tags( $string,
                                        $length = 50,
                                        $elision_string = '...' ) {
    $replace_function = create_function(
        '$matches',
        'return "<a href=\"$matches[0]\">" .
                abbreviated_url( $matches[ 0 ], '            .
                                 $length  . ', '             .
                                 '"' . $elision_string . '"' .
                               ') . "</a>";'
    );
    return preg_replace_callback(
        URL_REGEX,
        $replace_function,
        $string
    );
}

, URL- regex, , DaveRandom, . , : "/" (: [\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#]). , , 80 8080.

, .

+1

, , ,

(http|https|ftp):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?
0

All Articles