Created
December 9, 2016 00:54
-
-
Save davidcostadev/9aff8f6d44521d0afec60d466d196a8d to your computer and use it in GitHub Desktop.
Converter string para urls amigaveis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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