Guest User

Untitled

a guest
Jun 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. <?php
  2.  
  3. function slugify($value, $transliteration = false)
  4. {
  5. if (extension_loaded('intl') && $transliteration == true) {
  6. $transliterator = \Transliterator::create('Any-Latin; Latin-ASCII');
  7. $value = $transliterator->transliterate($value);
  8. }
  9.  
  10. //
  11. $str = preg_replace('/\s+/', '-', trim($value)); // Trim and remove spaces
  12. $str = str_replace('_', '-', $str); // Underscores to dashes
  13. $str = preg_replace('/[^\pL0-9-]/u', '', strtolower($str)); // Only alpha-numeric and dashes are permitted
  14. $str = preg_replace('/-+/', '-', $str); // Prevent 2+ dashes from appearing together
  15.  
  16. // Don't end in a dash
  17. if( substr($str, -1, 1) === '-' ){
  18. $str = substr($str, 0, -1);
  19. }
  20.  
  21. return $str;
  22. }
Add Comment
Please, Sign In to add comment