generates slug or permalink from a given string


/ Published in: PHP
Save to your folder(s)

Esta versión, a diferencia de mi otra función reemplaza las vocales acentuadas y otros caracteres


Copy this code and paste it in your HTML
  1. /**
  2.  * Rewrite strings to be URL/SEO friendly
  3.  * @param string $text
  4.  * @return string
  5.  */
  6. function slugify($text) {
  7. // Map thanks to CakePHP
  8. $map = array(
  9. '/à|á|å|â/' => 'a',
  10. '/è|é|ê|?|ë/' => 'e',
  11. '/ì|í|î/' => 'i',
  12. '/ò|ó|ô|ø/' => 'o',
  13. '/ù|ú|?|û/' => 'u',
  14. '/ç/' => 'c',
  15. '/ñ/' => 'n',
  16. '/ä|æ/' => 'ae',
  17. '/ö/' => 'oe',
  18. '/ü/' => 'ue',
  19. '/�/' => 'Ae',
  20. '/�/' => 'Ue',
  21. '/�/' => 'Oe',
  22. '/�/' => 'ss',
  23. '/[^\w\s]/' => ' ',
  24. );
  25.  
  26. $text = preg_replace(array_keys($map), array_values($map), $text);
  27. $text = preg_replace('/[^-a-zA-Z0-9&\s]/i', '', $text);
  28. $text = trim(strtolower($text));
  29. $text = str_replace(array('-', ' ', '&'), array('_', '-', 'and'), $text);
  30. $text = urlencode($text);
  31.  
  32. return $text;
  33. }

URL: http://www.milesj.me/blog/read/15/5-custom-basic-php-string-functions

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.