Created
June 28, 2013 18:44
-
-
Save cagartner/5887009 to your computer and use it in GitHub Desktop.
Função para criar url amigável PHP
This file contains hidden or 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
static function urlAmigavel($nom_tag,$slug="-") { | |
$string = strtolower($nom_tag); | |
// Código ASCII das vogais | |
$ascii['a'] = range(224, 230); | |
$ascii['e'] = range(232, 235); | |
$ascii['i'] = range(236, 239); | |
$ascii['o'] = array_merge(range(242, 246), array(240, 248)); | |
$ascii['u'] = range(249, 252); | |
// Código ASCII dos outros caracteres | |
$ascii['b'] = array(223); | |
$ascii['c'] = array(231); | |
$ascii['d'] = array(208); | |
$ascii['n'] = array(241); | |
$ascii['y'] = array(253, 255); | |
foreach ($ascii as $key=>$item) { | |
$acentos = ''; | |
foreach ($item AS $codigo) $acentos .= chr($codigo); | |
$troca[$key] = '/['.$acentos.']/i'; | |
} | |
$string = preg_replace(array_values($troca), array_keys($troca), $string); | |
// Slug? | |
if ($slug) { | |
// Troca tudo que não for letra ou número por um caractere ($slug) | |
$string = preg_replace('/[^a-z0-9]/i', $slug, $string); | |
// Tira os caracteres ($slug) repetidos | |
$string = preg_replace('/' . $slug . '{2,}/i', $slug, $string); | |
$string = trim($string, $slug); | |
} | |
return $string; | |
} |
Parabéns meu caro, devo confessar que te amo
Show de bola
Muito boa! Estou utilizando!
😄
Parabéns, me ajudou muito
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Parabéns pela funções, muito usado nos meus projetos!