Last active
February 27, 2023 20:51
-
-
Save Marlysson/bcd96e1d9394624936444b226583bfae to your computer and use it in GitHub Desktop.
Script php para criar slug de um texto dado
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 | |
class Slugfy{ | |
public static function getSlug($string){ | |
$string = stripslashes($string); | |
$string = preg_replace('/[[:punct:]]+/',' ',$string); | |
$string = preg_replace('/[[:space:]]+/',' ',$string); | |
$string = ltrim($string,". "); | |
$string = rtrim($string,". "); | |
$replaces = array ( | |
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A','Ă' => 'A', 'Æ' => 'AE', | |
'Ç' =>'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', | |
'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', | |
'Ő' => 'O', 'Ø' => 'O', 'Œ' => 'OE' ,'Ș' => 'S','Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', | |
'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', | |
'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'ă' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', | |
'ê' => 'e', 'ë' => 'e','ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', | |
'ò' => 'o', 'ó' =>'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', 'ø' => 'o', 'œ' => 'oe', | |
'ș' => 's', 'ț' => 't', 'ù' => 'u', 'ú' => 'u','û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', | |
'þ' => 'th', 'ÿ' => 'y' | |
); | |
$replaced = strtr($string,$replaces); | |
$split = explode(" ",$replaced); | |
$lower_words = array(); | |
foreach($split as $word){ | |
array_push($lower_words, strtolower($word)); | |
} | |
return implode("-",$lower_words); | |
} | |
} | |
echo Slugfy::getSlug("Novos lançamentos da música.!").PHP_EOL; | |
echo Slugfy::getSlug("Novas notícias do telejornal.").PHP_EOL; | |
echo Slugfy::getSlug("Compre já o livro padrões de refatorações").PHP_EOL; | |
echo Slugfy::getSlug("Não perca a maior feira de lojas ... ").PHP_EOL; | |
echo Slugfy::getSlug("Não perca / a maior feira / de lojas ").PHP_EOL; | |
echo Slugfy::getSlug("Não perca | a maior feira de lojas \ \ ").PHP_EOL; | |
#Sáídas | |
''' | |
novos-lancamentos-da-musica | |
novas-noticias-do-telejornal | |
compre-ja-o-livro-padroes-de-refatoracoes | |
nao-perca-a-maior-feira-de-lojas | |
nao-perca-a-maior-feira-de-lojas | |
nao-perca-a-maior-feira-de-lojas | |
''' |
Você pode fazer isso com apenas uma linha^^, caso tenha dúvidas de como o código funciona, pode perguntar sem problemas que eu posso estar explicando. Pode testar, com ele você vai obter o mesmo resultado
return strtolower(preg_replace(['/[[:punct:]]+/', '/[[:space:]\s]+/'], ['', '-'], trim(stripslashes(iconv('UTF-8', 'ASCII//TRANSLIT', $string)))));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Melhoras