Skip to content

Instantly share code, notes, and snippets.

@daniloroddrigues
Last active July 10, 2021 19:05
Show Gist options
  • Save daniloroddrigues/635200c461e731ec362dda3b628afa90 to your computer and use it in GitHub Desktop.
Save daniloroddrigues/635200c461e731ec362dda3b628afa90 to your computer and use it in GitHub Desktop.
function tirarAcentos($string){
return preg_replace(array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/", "/�/"),explode(" ","a A e E i I o O u U n N 1"),$string);
}
function urlizer($string) {
$acentos = array(
'A' => '/À|Á|Â|Ã|Ä|Å/',
'a' => '/à|á|â|ã|ä|å/',
'C' => '/Ç/',
'c' => '/ç/',
'E' => '/È|É|Ê|Ë/',
'e' => '/è|é|ê|ë/',
'I' => '/Ì|Í|Î|Ï/',
'i' => '/ì|í|î|ï/',
'N' => '/Ñ/',
'n' => '/ñ/',
'O' => '/Ò|Ó|Ô|Õ|Ö/',
'o' => '/ò|ó|ô|õ|ö/',
'U' => '/Ù|Ú|Û|Ü/',
'u' => '/ù|ú|û|ü/',
'Y' => '/Ý/',
'y' => '/ý|ÿ/',
'a.' => '/ª/',
'o.' => '/º/'
);
$limpa = preg_replace($acentos, array_keys($acentos), htmlentities($string, ENT_NOQUOTES, "UTF-8"));
$arraySimbolos = array("'",":","\"","/","”","“",",","!","@","#","$","%","¨","&","*","(",")",";","<",">","?","|","º","+","-",".");
for($i = 0; $i < count($arraySimbolos); $i++){
$limpa = str_replace($arraySimbolos[$i],"",$limpa);
}
$limpa = strtolower($limpa);
$limpa = str_replace("--","-",$limpa);
$limpa = str_replace("---","-",$limpa);
$limpa = str_replace("----","-",$limpa);
$limpa = str_replace("-----","-",$limpa);
$limpa = str_replace("------","-",$limpa);
$limpa = str_replace(" "," ",$limpa);
$limpa = str_replace(" "," ",$limpa);
$limpa = str_replace(" "," ",$limpa);
$limpa = str_replace(" "," ",$limpa);
$limpa = str_replace(" ", "-", $limpa);
return $limpa;
}
function smartURL($str){
$str = strtolower(utf8_decode($str)); $i=1;
$str = strstr($str, utf8_decode('àáâãäåæçèéêëìíîïñòóôõöøùúûýýÿ'), 'aaaaaaaceeeeiiiinoooooouuuyyy');
// $str = strstr($str, utf8_decode('ÀÁÂÃÄåæçèéêëìíîïñòóôõöøùúûýýÿ'), 'aaaaaaaceeeeiiiinoooooouuuyyy');
$str = preg_replace("/([^a-z0-9])/",'-',utf8_encode($str));
while($i>0) $str = str_replace('--','-',$str,$i);
if (substr($str, -1) == '-') $str = substr($str, 0, -1);
return $str;
}
function slug($string) {
$table = array(
'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r', '/' => '-', ' ' => '-', '.' => '', '$'=>'S', ','=>'-'
);
// -- Remove duplicated spaces
$stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $string);
// -- Returns the slug
return strtolower(strtr($string, $table));
}
function detect_encoding($string) { ////w3.org/International/questions/qa-forms-utf-8.html
if (preg_match('%^(?: [\x09\x0A\x0D\x20-\x7E] | [\xC2-\xDF][\x80-\xBF] | \xE0[\xA0-\xBF][\x80-\xBF] | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} | \xED[\x80-\x9F][\x80-\xBF] | \xF0[\x90-\xBF][\x80-\xBF]{2} | [\xF1-\xF3][\x80-\xBF]{3} | \xF4[\x80-\x8F][\x80-\xBF]{2} )*$%xs', $string)) return 'UTF-8';
//If you need to distinguish between UTF-8 and ISO-8859-1 encoding, list UTF-8 first in your encoding_list.
//if you list ISO-8859-1 first, mb_detect_encoding() will always return ISO-8859-1.
return mb_detect_encoding($string, array('UTF-8', 'ASCII', 'ISO-8859-1', 'JIS', 'EUC-JP', 'SJIS'));
}
function convert_encoding($string, $to_encoding, $from_encoding = '') {
if ($from_encoding == '') {
$from_encoding = detect_encoding($string);
}
if ($from_encoding == $to_encoding) {
return $string;
}
return mb_convert_encoding($string, $to_encoding, $from_encoding);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment