Last active
July 14, 2020 13:09
-
-
Save jesobreira/d769aab503847e014b3c to your computer and use it in GitHub Desktop.
Create slugs with accented chars in PHP and Javascript
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
/* | |
* Javascript makeslug() | |
* by J. Santos <jefrey[at]jefrey[dot]ml> | |
*/ | |
/* | |
Usage: | |
string makeslug( string val [, string replaceBy = "-" ] ) | |
Example: | |
makeslug(" This is a string fuLL of áccêntèd and InVaLid (?) cHaRs!!! "); | |
Will return: | |
this-is-a-string-full-of-accented-and-invalid-chars | |
And: | |
makeslug(" This is a string fuLL of áccêntèd and InVaLid (?) cHaRs!!! ", "_"); | |
Will return: | |
this_is_a_string_full_of_accented_and_invalid_chars | |
*/ | |
function makeslug(val, replaceBy) { | |
replaceBy = replaceBy || '-'; | |
var mapaAcentosHex = { // by @marioluan and @lelotnk | |
a : /[\xE0-\xE6]/g, | |
A : /[\xC0-\xC6]/g, | |
e : /[\xE8-\xEB]/g, // if you're gonna echo this | |
E : /[\xC8-\xCB]/g, // JS code through PHP, do | |
i : /[\xEC-\xEF]/g, // not forget to escape these | |
I : /[\xCC-\xCF]/g, // backslashes (\), by repeating | |
o : /[\xF2-\xF6]/g, // them (\\) | |
O : /[\xD2-\xD6]/g, | |
u : /[\xF9-\xFC]/g, | |
U : /[\xD9-\xDC]/g, | |
c : /\xE7/g, | |
C : /\xC7/g, | |
n : /\xF1/g, | |
N : /\xD1/g, | |
}; | |
for ( var letra in mapaAcentosHex ) { | |
var expressaoRegular = mapaAcentosHex[letra]; | |
val = val.replace( expressaoRegular, letra ); | |
} | |
val = val.toLowerCase(); | |
val = val.replace(/[^a-z0-9\-]/g, " "); | |
val = val.replace(/ {2,}/g, " "); | |
val = val.trim(); | |
val = val.replace(/\s/g, replaceBy); | |
return val; | |
} |
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 | |
/* | |
* PHP makeslug() | |
* by J. Santos <jefrey[at]jefrey[dot]ml> | |
*/ | |
/* | |
Usage: | |
string makeslug( string val [, string replaceBy = "-" ] ) | |
Example: | |
makeslug(" This is a string fuLL of áccêntèd and InVaLid (?) cHaRs!!! "); | |
Will return: | |
this-is-a-string-full-of-accented-and-invalid-chars | |
And: | |
makeslug(" This is a string fuLL of áccêntèd and InVaLid (?) cHaRs!!! ", "_"); | |
Will return: | |
this_is_a_string_full_of_accented_and_invalid_chars | |
*/ | |
function makeslug($val, $replaceBy = '-') { | |
if(mb_detect_encoding($val.'x', 'UTF-8, ISO-8859-1') == 'UTF-8') | |
$val = utf8_decode(strtolower($val)); | |
$mapaAcentosHex = Array( // by @marioluan and @lelotnk | |
'a' => '/[\\xE0-\\xE6]/', | |
'A' => '/[\\xC0-\\xC6]/', | |
'e' => '/[\\xE8-\\xEB]/', | |
'E' => '/[\\xC8-\\xCB]/', | |
'i' => '/[\\xEC-\\xEF]/', | |
'I' => '/[\\xCC-\\xCF]/', | |
'o' => '/[\\xF2-\\xF6]/', | |
'O' => '/[\\xD2-\\xD6]/', | |
'u' => '/[\\xF9-\\xFC]/', | |
'U' => '/[\\xD9-\\xDC]/', | |
'c' => '/\\xE7/', | |
'C' => '/\\xC7/', | |
'n' => '/\\xF1/', | |
'N' => '/\\xD1/' | |
); | |
foreach($mapaAcentosHex as $letra => $expressaoRegular) { | |
$val = preg_replace( $expressaoRegular, $letra, $val ); | |
} | |
$val = strtolower($val); | |
$val = preg_replace('/[^a-z0-9\-]/', " ", $val); | |
$val = preg_replace("/ {2,}/", " ", $val); | |
$val = trim($val); | |
$val = str_replace(" ", $replaceBy, $val); | |
return $val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice!