Exemple #1
0
 /**
  * Create a web friendly URL slug from a string.
  *
  * Although supported, transliteration is discouraged because
  * 1) most web browsers support UTF-8 characters in URLs
  * 2) transliteration causes a loss of information
  *
  * @author Sean Murphy <*****@*****.**>
  * @copyright Copyright 2012 Sean Murphy. All rights reserved.
  * @license http://creativecommons.org/publicdomain/zero/1.0/
  *
  * @param string $str
  * @param array $options
  * 
  * @return string
  */
 public static function build($str, $options = array())
 {
     $str = mb_convert_encoding((string) $str, 'UTF-8', mb_list_encodings());
     $defaults = array('delimiter' => '-', 'limit' => null, 'lowercase' => true, 'replacements' => array(), 'transliterate' => true);
     // Merge options
     $options = array_merge($defaults, $options);
     // Make custom replacements
     $str = StringFilter::customReplace($str, $options['replacements']);
     // Transliterate characters to ASCII
     if ($options['transliterate']) {
         $str = StringTransformer::transliterateToASCII($str);
     }
     // Replace non-alphanumeric characters with our delimiter
     $str = preg_replace('/[^\\p{L}\\p{Nd}]+/u', $options['delimiter'], $str);
     // Remove duplicate delimiters
     $str = preg_replace('/(' . preg_quote($options['delimiter'], '/') . '){2,}/', '$1', $str);
     // Truncate slug to max. characters
     $str = mb_substr($str, 0, $options['limit'] ? $options['limit'] : mb_strlen($str, 'UTF-8'), 'UTF-8');
     // Remove delimiter from ends
     $str = trim($str, $options['delimiter']);
     return $options['lowercase'] ? StringTransformer::toLowercase($str) : $str;
 }