Changeset 541 for trunk/lib/Utilities.inc.php
- Timestamp:
- Aug 12, 2015 12:22:54 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Utilities.inc.php
r534 r541 215 215 * @access public 216 216 * @param string $text Text to search for URLs. 217 * @param bool $strict True to only include URLs starting with a scheme (http:// ftp:// im://), or false to include URLs starting with 'www.'. 217 218 * @param mixed $length Number of characters to truncate URL, or NULL to disable truncating. 218 219 * @param string $delim Delimiter to append, indicate truncation. 219 220 * @return string Same input text, but URLs hyperlinked. 220 221 * @author Quinn Comendant <quinn@strangecode.com> 221 * @version 1.0222 * @version 2.0 222 223 * @since 22 Mar 2015 23:29:04 223 224 */ 224 function hyperlinkTxt($text, $length=null, $delim='âŠ') 225 { 226 return preg_replace_callback( 227 // Inspired by @stephenhay's regex from https://mathiasbynens.be/demo/url-regex 228 // Here we capture the full URL into the first match and only the first X characters into the second match. 229 sprintf('@\b(?<!")(?<!\')(?<!=)(((?:https?|s?ftps?)://[^\s/$.?#].[^\s]{0,%s})[^\s]*)@iS', $length), 230 // Use an anonymous function to decide when to append the delim. 231 // Also encode special chars with oTxt(). 232 function ($m) use ($length, $delim) { 233 if (is_null($length) || $m[1] == $m[2]) { 234 // If not truncating, or URL was not truncated. 235 return sprintf('<a href="%s">%s</a>', oTxt($m[1]), oTxt($m[1])); 236 } else { 237 // Truncated URL. 238 return sprintf('<a href="%s">%s%s</a>', oTxt($m[1]), oTxt(trim($m[2])), $delim); 239 } 240 }, 241 $text 225 function hyperlinkTxt($text, $strict=false, $length=null, $delim='âŠ') 226 { 227 // Capture the full URL into the first match and only the first X characters into the second match. 228 // This will match URLs not preceeded by " ' or = (URLs inside an attribute) or ` (Markdown quoted) or double-scheme (http://http://www.asdf.com) 229 // Valid URL characters: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;= 230 $regex = '@ 231 \b # Start with a word-boundary. 232 (?<!"|\'|=|>|`|[\w-]{2}://) # Negative look-behind to exclude URLs already in <a> tag, Markdown quoted, or double SCHEME:// 233 ( # Begin match 1 234 ( # Begin match 2 235 (?:[\w-]{2,}://%s) # URL starts with SCHEME:// or www. (if strict = false) 236 [^\s/$.?#]+ # Any domain-valid characters 237 \. # At least one point 238 [^\s"`<>]{1,%s} # Match 2 is limited to a maximum of LENGTH valid URL characters 239 ) 240 [^\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 242 ) 243 @Suxi 244 '; 245 $regex = sprintf($regex, 246 ($strict ? '' : '|www\.'), // Strict=false allows URLs beginning with www. 247 $length, 248 ($strict ? '' : '?!.,:;)\'-') // Strict=false excludes these characters from set of the last character of URL. 242 249 ); 250 251 // Use a callback function to decide when to append the delim. 252 // Also encode special chars with oTxt(). 253 return preg_replace_callback($regex, function ($m) use ($length, $delim) { 254 $url = $m[1]; 255 $truncated_url = $m[2]; 256 $absolute_url = preg_replace('!^www\.!', 'http://www.', $url); 257 if (is_null($length) || $url == $truncated_url) { 258 // If not truncating, or URL was not truncated. 259 $display_url = preg_replace('!^[\w-]{2,}://!', '', $url); 260 return sprintf('<a href="%s">%s</a>', oTxt($absolute_url), $display_url); 261 } else { 262 // Truncated URL. 263 $display_url = preg_replace('!^[\w-]{2,}://!', '', trim($truncated_url)); 264 return sprintf('<a href="%s">%s%s</a>', oTxt($absolute_url), $display_url, $delim); 265 } 266 }, $text); 243 267 } 244 268 … … 452 476 function URLSlug($str) 453 477 { 454 $slug = preg_replace(array('/ [^\w]+/', '/^-+|-+$/'), array('-', ''), $str);478 $slug = preg_replace(array('/\W+/u', '/^-+|-+$/'), array('-', ''), $str); 455 479 $slug = strtolower($slug); 456 480 return $slug;
Note: See TracChangeset
for help on using the changeset viewer.