Skip to content

Instantly share code, notes, and snippets.

@davidcostadev
Created December 9, 2016 00:54
Show Gist options
  • Save davidcostadev/9aff8f6d44521d0afec60d466d196a8d to your computer and use it in GitHub Desktop.
Save davidcostadev/9aff8f6d44521d0afec60d466d196a8d to your computer and use it in GitHub Desktop.
Converter string para urls amigaveis
<?php
/**
* Convertendo textos para urlamigavel
* @param type $texto
* @return String
*/
function toamigavel($texto) {
$texto = mb_strtolower($texto, 'UTF-8');
$acentos = array(
'a' => '/À|Á|Â|Ã|Ä|Å/',
'a' => '/à|á|â|ã|ä|å/',
'c' => '/Ç/',
'c' => '/ç/',
'e' => '/È|É|Ê|Ë/',
'e' => '/è|é|ê|ë/',
'i' => '/Ì|Í|Î|Ï/',
'i' => '/ì|í|î|ï/',
'n' => '/Ñ/',
'n' => '/ñ/',
'o' => '/Ò|Ó|Ô|Õ|Ö/',
'o' => '/ò|ó|ô|õ|ö/',
'u' => '/Ù|Ú|Û|Ü/',
'u' => '/ù|ú|û|ü/',
'y' => '/Ý/',
'y' => '/ý|ÿ/',
'a.' => '/ª/',
'o.' => '/º/');
$texto = preg_replace($acentos, array_keys($acentos), $texto);
$texto = preg_replace('/[^a-z0-9- \/.]/i', '', $texto);//Remover caricteries indesejaveis
$texto = preg_replace('/[^a-z0-9]{0,}[^a-z0-9]/', '-', $texto);
$texto = preg_replace('/[^a-z0-9]{0,}[^a-z0-9]/', '-', $texto);
return $texto;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment