Changeset 545 for trunk/lib


Ignore:
Timestamp:
Aug 19, 2015 4:18:55 AM (9 years ago)
Author:
anonymous
Message:

Updated hyperlinkTxt function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Utilities.inc.php

    r544 r545  
    225225function hyperlinkTxt($text, $strict=false, $length=null, $delim='
')
    226226{
     227    // A list of schemes we allow at the beginning of a URL.
     228    $schemes = 'mailto:|tel:|skype:|callto:|facetime:|bitcoin:|geo:|magnet:\?|sip:|sms:|xmpp:|view-source:(?:https?://)?|[\w-]{2,}://';
     229
    227230    // Capture the full URL into the first match and only the first X characters into the second match.
    228231    // This will match URLs not preceeded by " ' or = (URLs inside an attribute) or ` (Markdown quoted) or double-scheme (http://http://www.asdf.com)
     
    233236        (                               # Begin match 1
    234237            (                           # Begin match 2
    235                 (?:[\w-]{2,}://%s)      # URL starts with SCHEME:// or www. (if strict = false)
     238                (?:%s)                  # URL starts with known scheme or www. if strict = false
    236239                [^\s/$.?#]+             # Any domain-valid characters
    237                 \.                      # At least one point
    238240                [^\s"`<>]{1,%s}         # Match 2 is limited to a maximum of LENGTH valid URL characters
    239241            )
    240242            [^\s"`<>]*                  # Match 1 continues with any further valid URL characters
    241             [^\P{Any}\s
<>«»"—–%s]      # Final character not a space or common end-of-sentence punctuation (.,:;?!, etc). Using double negation set, see http://stackoverflow.com/a/4786560/277303
     243            ([^\P{Any}\s
<>«»"—–%s])    # Final character not a space or common end-of-sentence punctuation (.,:;?!, etc). Using double negation set, see http://stackoverflow.com/a/4786560/277303
    242244        )
    243245        @Suxi
    244246    ';
    245247    $regex = sprintf($regex,
    246         ($strict ? '' : '|www\.'), // Strict=false allows URLs beginning with www.
    247         $length,
    248         ($strict ? '' : '?!.,:;)\'-') // Strict=false excludes these characters as a possible last character of URL.
     248        ($strict ? $schemes : $schemes . '|www\.'), // Strict=false adds "www." to the list of allowed start-of-URL.
     249        ($length ? $length : ''),
     250        ($strict ? '' : '?!.,:;)\'-') // Strict=false excludes some "URL-valid" characters from the last character of URL. (Hyphen must remain last character in this class.)
    249251    );
    250252
     
    253255    return preg_replace_callback($regex, function ($m) use ($length, $delim) {
    254256        $url = $m[1];
    255         $truncated_url = $m[2];
     257        $truncated_url = $m[2] . $m[3];
    256258        $absolute_url = preg_replace('!^www\.!', 'http://www.', $url);
    257259        if (is_null($length) || $url == $truncated_url) {
    258260            // If not truncating, or URL was not truncated.
    259             $display_url = preg_replace('!^[\w-]{2,}://!', '', $url);
     261            // Remove http schemas, and any single trailing / to make the display URL.
     262            $display_url = preg_replace(['!^https?://!', '!^([^/]+)/$!'], ['', '$1'], $url);
    260263            return sprintf('<a href="%s">%s</a>', oTxt($absolute_url), $display_url);
    261264        } else {
    262265            // Truncated URL.
    263             $display_url = preg_replace('!^[\w-]{2,}://!', '', trim($truncated_url));
     266            // Remove http schemas, and any single trailing / to make the display URL.
     267            $display_url = preg_replace(['!^https?://!', '!^([^/]+)/$!'], ['', '$1'], trim($truncated_url));
    264268            return sprintf('<a href="%s">%s%s</a>', oTxt($absolute_url), $display_url, $delim);
    265269        }
Note: See TracChangeset for help on using the changeset viewer.