Changeset 505 for trunk/lib/Utilities.inc.php
- Timestamp:
- Mar 23, 2015 10:06:42 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Utilities.inc.php
r502 r505 135 135 136 136 /** 137 * Returns text with appropriate html translations .137 * Returns text with appropriate html translations (a smart wrapper for htmlspecialchars()). 138 138 * 139 139 * @param string $text Text to clean. … … 208 208 209 209 return preg_replace($search, $replace, $text); 210 } 211 212 /* 213 * Finds all URLs in text and hyperlinks them. 214 * 215 * @access public 216 * @param string $text Text to search for URLs. 217 * @param mixed $length Number of characters to truncate URL, or NULL to disable truncating. 218 * @param string $delim Delimiter to append, indicate truncation. 219 * @return string Same input text, but URLs hyperlinked. 220 * @author Quinn Comendant <quinn@strangecode.com> 221 * @version 1.0 222 * @since 22 Mar 2015 23:29:04 223 */ 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 242 ); 210 243 } 211 244
Note: See TracChangeset
for help on using the changeset viewer.