Changeset 722 for trunk/lib/Utilities.inc.php
- Timestamp:
- May 3, 2020 9:58:17 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Utilities.inc.php
r718 r722 520 520 } 521 521 522 /*523 * Converts a string into a URL-safe slug, removing spaces and non word characters.524 *525 * @access public526 * @param string $str String to convert.527 * @return string URL-safe slug.528 * @author Quinn Comendant <quinn@strangecode.com>529 * @version 1.0530 * @since 18 Aug 2014 12:54:29531 */532 function URLSlug($str)533 {534 $slug = preg_replace(array('/\W+/u', '/^-+|-+$/u'), array('-', ''), $str);535 $slug = strtolower($slug);536 return $slug;537 }538 539 522 /** 540 523 * Return a human readable disk space measurement. Input value measured in bytes. … … 623 606 } 624 607 625 /** 626 * Removes non-latin characters from file name, using htmlentities to convert known weirdos into regular squares. 608 /* 609 * Converts strange characters into ASCII using a htmlentities hack. If a character does not have a specific rule, it will remain as its entity name, e.g., `5¢` becomes `5¢` which becomes `5cent`. 610 * 611 * @access public 612 * @param string $str Input string of text containing accents. 613 * @return string String with accented characters converted to ASCII equivalents. 614 * @author Quinn Comendant <quinn@strangecode.com> 615 * @since 30 Apr 2020 21:29:16 616 */ 617 function simplifyAccents($str) 618 { 619 $app =& App::getInstance(); 620 621 return preg_replace([ 622 '/&(?=[\w\d#]{1,10};)/ui', 623 '/&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/ui', 624 '/&(?:ndash|mdash|horbar);/ui', 625 '/&(?:nbsp);/ui', 626 '/&(?:bdquo|ldquo|ldquor|lsquo|lsquor|rdquo|rdquor|rsquo|rsquor|sbquo|lsaquo|rsaquo);/ui', 627 '/&(?:amp);/ui', // This replacement must come after matching all other entities. 628 '/[&;]+/u', 629 ], [ 630 '&', 631 '$1', 632 '-', 633 ' ', 634 '', 635 'and', 636 '', 637 ], htmlentities($str, ENT_NOQUOTES | ENT_IGNORE, $app->getParam('character_set'))); 638 } 639 640 /* 641 * Converts a string into a URL-safe slug, removing spaces and non word characters. 642 * 643 * @access public 644 * @param string $str String to convert. 645 * @return string URL-safe slug. 646 * @author Quinn Comendant <quinn@strangecode.com> 647 * @version 1.0 648 * @since 18 Aug 2014 12:54:29 649 */ 650 function URLSlug($str) 651 { 652 return strtolower(urlencode(preg_replace(['/[-\sââ.:;?!@#=+_\/\\\]+|(?: | |–|–|—|—|%c2%a0|%e2%80%93|%e2%80%9)+/u', '/-+/u', '/[^\w-]+/u', '/^-+|-+$/u'], ['-', '-', '', ''], simplifyAccents($str)))); 653 } 654 655 /** 656 * Converts a string of text into a safe file name by removing non-ASCII characters and non-word characters. 627 657 * 628 658 * @access public … … 634 664 $app =& App::getInstance(); 635 665 636 $file_name = preg_replace(array( 637 '/&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/ui', 638 '/&(?:amp);/ui', 639 '/[&;]+/u', 640 '/[^a-zA-Z0-9()@._=+-]+/u', 641 '/^_+|_+$/u' 642 ), array( 643 '$1', 644 'and', 645 '', 646 '_', 647 '' 648 ), htmlentities($file_name, ENT_NOQUOTES | ENT_IGNORE, $app->getParam('character_set'))); 666 $file_name = preg_replace(['/[^a-zA-Z0-9()@._=+-]+/u', '/^_+|_+$/u'], ['_', ''], simplifyAccents($file_name)); 649 667 return mb_substr($file_name, 0, 250); 650 668 }
Note: See TracChangeset
for help on using the changeset viewer.