Changeset 545 for trunk/lib/Utilities.inc.php
- Timestamp:
- Aug 19, 2015 4:18:55 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Utilities.inc.php
r544 r545 225 225 function hyperlinkTxt($text, $strict=false, $length=null, $delim='âŠ') 226 226 { 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 227 230 // Capture the full URL into the first match and only the first X characters into the second match. 228 231 // This will match URLs not preceeded by " ' or = (URLs inside an attribute) or ` (Markdown quoted) or double-scheme (http://http://www.asdf.com) … … 233 236 ( # Begin match 1 234 237 ( # 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 236 239 [^\s/$.?#]+ # Any domain-valid characters 237 \. # At least one point238 240 [^\s"`<>]{1,%s} # Match 2 is limited to a maximum of LENGTH valid URL characters 239 241 ) 240 242 [^\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/277303243 ([^\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 242 244 ) 243 245 @Suxi 244 246 '; 245 247 $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.) 249 251 ); 250 252 … … 253 255 return preg_replace_callback($regex, function ($m) use ($length, $delim) { 254 256 $url = $m[1]; 255 $truncated_url = $m[2] ;257 $truncated_url = $m[2] . $m[3]; 256 258 $absolute_url = preg_replace('!^www\.!', 'http://www.', $url); 257 259 if (is_null($length) || $url == $truncated_url) { 258 260 // 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); 260 263 return sprintf('<a href="%s">%s</a>', oTxt($absolute_url), $display_url); 261 264 } else { 262 265 // 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)); 264 268 return sprintf('<a href="%s">%s%s</a>', oTxt($absolute_url), $display_url, $delim); 265 269 }
Note: See TracChangeset
for help on using the changeset viewer.