usuwanie znaków specjalnych z kodowania iso-8859-2 latin2
Często zachodzi potrzeba zamiany ogonków na ich graficzne odpowiedniki bez ogonków w kodowaniu ASCII. Najczęściej służy to do przygotowania tekstu wo wyświetlenia w adresie url – uri. Lecz niestety jest to zależne od użytego kodowania na stronie jak i bazie danych MYSQL.
Ponieważ najczęściej wykorzystujemy podmianę znaków specjalnych, narodowych, diakrytycznych w przygotowywaniu przyjaznych adresów – friendly URL ze słowami kluczowymi dlatego też potrzebujemy dynamicznej funkcji która to zrobi za nas automatycznie.
Tutaj prezentuję tablice oraz funkcje specjalnie dla kodowania iso-8859-2, znanego pod nazwą latin2, jeszcze inna nazwa to ISO/IEC 8859-2 jak i kodowanie środkowoeuropejskie oraz wschodnioeuropejskie. Może jest to dosć zacofane kodowanie lecz nadal często używane na stronach WWW oraz bazach mysql (latin2) ze względu na obsługę polskich znaków diakrytycznych.
Kodowanie iso-8859-2 obsługuje takie języki jak (pod względem znaków narodowych – specjalnych):
- Polski
- Bośniacki
- Chorwacki
- Czeski
- Węgierski
- Serbski (w transkrypcji łacińskiej)
- Serbsko-Chorwacki
- Słowacki
- Słoweński
- Górnołużycki
- Dolnołużycki
- Angielski
- Niemiecki
- sporną kwestią jest język Fiński
Jednakże zalecą się stosowanie kodowań zgodnych z Unicode np. utf-8, ponieważ mimo iż iso-8859-2 wydaje się obsługiwać wiele języków obsługuje ono tylko podstawy i w swoim asortymencie posiada tylko niewiele znaków specjalnych, oraz cała tablica kodowania jest skromna.
O co chodzi w tej konwersji iso-8859-2 to ASCII, otóż o to by zamienić np. nasze polskie ć na jego odpowiednik c który może być obsłużony w adresach URL przez przeglądarki i niestety trzeba postąpić tak z każdym zadaszkowanym i ogonkowym znakiem. Ř -> R i tym podobne…
Poniżej przedstawiam tablice do użytku nie komercyjnego, które ostatnio sobie stworzyłem do przygotowania przyjaznych adresów w kodowaniu iso-8859-2. Są niewrażliwe na kodowanie pliku w którym są zapisane ponieważ korzystają z wartości heksadecymalnych popularnie zwane HEX. Zamieniają każdy znak na jego graficzny odpowiednik, działają z wielkimi jak i małymi literami! Oczywiście zamieniają także polskie znaki!
$map_latin2=array(
"\xa1","\xa3","\xa5","\xa6","\xa7","\xa9","\xaa","\xab","\xac","\xae","\xaf",
"\xb1","\xb3","\xb5","\xb6","\xb9","\xba","\xbb","\xbc","\xbe","\xbf","\xc0",
"\xc1","\xc2","\xc3","\xc4","\xc5","\xc6","\xc7","\xc8","\xc9","\xca","\xcb",
"\xcc","\xcd","\xce","\xcf","\xd0","\xd1","\xd2","\xd3","\xd4","\xd5","\xd6",
"\xd7","\xd8","\xd9","\xda","\xdb","\xdc","\xdd","\xde","\xdf","\xe0","\xe1",
"\xe2","\xe3","\xe4","\xe5","\xe6","\xe7","\xe8","\xe9","\xea","\xeb","\xec",
"\xed","\xee","\xef","\xf0","\xf1","\xf2","\xf3","\xf4","\xf5","\xf6","\xf8",
"\xf9","\xfa","\xfb","\xfc","\xfd","\xfe",
);
//Tablice znakow diakrytycznych
//dla kodowania iso-8859-2 (latin2)
//by tosiek - https://tosiek.pl/
$map2_latin2=array(
"\x41","\x4c","\x4c","\x53","\x53","\x53","\x53","\x54","\x5a","\x5a","\x5a",
"\x61","\x6c","\x6c","\x73","\x73","\x73","\x74","\x7a","\x7a","\x7a","\x52",
"\x41","\x41","\x41","\x41","\x4c","\x43","\x43","\x43","\x45","\x45","\x45",
"\x45","\x49","\x49","\x44","\x44","\x4e","\x4e","\x4f","\x4f","\x4f","\x4f",
"\x78","\x52","\x55","\x55","\x55","\x55","\x59","\x54","\x73\x73","\x72","\x61",
"\x61","\x61","\x61","\x6c","\x63","\x63","\x63","\x65","\x65","\x65","\x65",
"\x69","\x69","\x64","\x64","\x6e","\x6e","\x6f","\x6f","\x6f","\x6f","\x72",
"\x75","\x75","\x75","\x75","\x79","\x74",
); |
$map_latin2=array("\xa1","\xa3","\xa5","\xa6","\xa7","\xa9","\xaa","\xab","\xac","\xae","\xaf","\xb1","\xb3","\xb5","\xb6","\xb9","\xba","\xbb","\xbc","\xbe","\xbf","\xc0","\xc1","\xc2","\xc3","\xc4","\xc5","\xc6","\xc7","\xc8","\xc9","\xca","\xcb","\xcc","\xcd","\xce","\xcf","\xd0","\xd1","\xd2","\xd3","\xd4","\xd5","\xd6","\xd7","\xd8","\xd9","\xda","\xdb","\xdc","\xdd","\xde","\xdf","\xe0","\xe1","\xe2","\xe3","\xe4","\xe5","\xe6","\xe7","\xe8","\xe9","\xea","\xeb","\xec","\xed","\xee","\xef","\xf0","\xf1","\xf2","\xf3","\xf4","\xf5","\xf6","\xf8","\xf9","\xfa","\xfb","\xfc","\xfd","\xfe",);//Tablice znakow diakrytycznych //dla kodowania iso-8859-2 (latin2)//by tosiek - https://tosiek.pl/$map2_latin2=array("\x41","\x4c","\x4c","\x53","\x53","\x53","\x53","\x54","\x5a","\x5a","\x5a","\x61","\x6c","\x6c","\x73","\x73","\x73","\x74","\x7a","\x7a","\x7a","\x52","\x41","\x41","\x41","\x41","\x4c","\x43","\x43","\x43","\x45","\x45","\x45","\x45","\x49","\x49","\x44","\x44","\x4e","\x4e","\x4f","\x4f","\x4f","\x4f","\x78","\x52","\x55","\x55","\x55","\x55","\x59","\x54","\x73\x73","\x72","\x61","\x61","\x61","\x61","\x6c","\x63","\x63","\x63","\x65","\x65","\x65","\x65","\x69","\x69","\x64","\x64","\x6e","\x6e","\x6f","\x6f","\x6f","\x6f","\x72","\x75","\x75","\x75","\x75","\x79","\x74",);
Na pierwszy rzut oka nie są czytelne, lecz poniżej prezentuję ich wersję z której odradzam korzystać, ponieważ taki plik z poniższymi tablicami musi być bezwzględnie zapisany w kodowaniu iso-8859-2 (trzeba się pilnować), także do użytku niekomercyjnego:
$map_latin2=array(
'Ą','Ł','Ľ','Ś','§','Š','Ş','Ť','Ź','Ž','Ż',
'ą','ł','ľ','ś','š','ş','ť','ź','ž','ż','Ŕ',
'Á','Â','Ă','Ä','Ĺ','Ć','Ç','Č','É','Ę','Ë',
'Ě','Í','Î','Ď','Đ','Ń','Ň','Ó','Ô','Ő','Ö',
'×','Ř','Ů','Ú','Ű','Ü','Ý','Ţ','ß','ŕ','á',
'â','ă','ä','ĺ','ć','ç','č','é','ę','ë','ě',
'í','î','ď','đ','ń','ň','ó','ô','ő','ö','ř',
'ů','ú','ű','ü','ý','ţ',
);
//Tablice znakow diakrytycznych
//dla kodowania iso-8859-2 (latin2)
//by tosiek - https://tosiek.pl/
$map2_latin2=array(
'A','L','L','S','S','S','S','T','Z','Z','Z',
'a','l','l','s','s','s','t','z','z','z','R',
'A','A','A','A','L','C','C','C','E','E','E',
'E','I','I','D','D','N','N','O','O','O','O',
'x','R','U','U','U','U','Y','T','ss','r','a',
'a','a','a','l','c','c','c','e','e','e','e',
'i','i','d','d','n','n','o','o','o','o','r',
'u','u','u','u','y','t',
); |
$map_latin2=array('Ą','Ł','Ľ','Ś','§','Š','Ş','Ť','Ź','Ž','Ż','ą','ł','ľ','ś','š','ş','ť','ź','ž','ż','Ŕ','Á','Â','Ă','Ä','Ĺ','Ć','Ç','Č','É','Ę','Ë','Ě','Í','Î','Ď','Đ','Ń','Ň','Ó','Ô','Ő','Ö','×','Ř','Ů','Ú','Ű','Ü','Ý','Ţ','ß','ŕ','á','â','ă','ä','ĺ','ć','ç','č','é','ę','ë','ě','í','î','ď','đ','ń','ň','ó','ô','ő','ö','ř','ů','ú','ű','ü','ý','ţ',);//Tablice znakow diakrytycznych //dla kodowania iso-8859-2 (latin2)//by tosiek - https://tosiek.pl/$map2_latin2=array('A','L','L','S','S','S','S','T','Z','Z','Z','a','l','l','s','s','s','t','z','z','z','R','A','A','A','A','L','C','C','C','E','E','E','E','I','I','D','D','N','N','O','O','O','O','x','R','U','U','U','U','Y','T','ss','r','a','a','a','a','l','c','c','c','e','e','e','e','i','i','d','d','n','n','o','o','o','o','r','u','u','u','u','y','t',);
Posiadając tablice znaków diakrytycznych możemy zamienić te znaki na ich odpowiedniki – funkcja str_replace(), lecz to nie jest całkowite przygotowanie adresów URL należy się także pozbyć znaków specjalnych, przestankowych i tym podobnych, które nie są obsługiwane w adresach. Tutaj wykorzystamy wyrażenia regularne (regular expressions) – funkcja preg_replace(), które zamienią każdy znak nie wchodzący w skład wyrazów na myślniki (-) lub podkreślniki – znaki podkreślenia (_), zależy kto jak woli, następnie musimy usunąć te znaki z początku i końca, wykorzystamy funkcję trim() z dodatkowym parametrem.
Ci którzy chcą by adresy były złożony tylko z małych liter muszą użyć funkcji strtolower()
Adresy URL – URI ze słowami kluczowymi
Wiec zaczynamy sobie pisać funkcję specialchar_latin2() która jako jedyny argument przyjmuje ciąg który przygotowuje do wyświetlenia adresu URL:
function specialchar_latin2($url) {
//specialchar_latin2 by tosiek https://tosiek.pl/
global $map_latin2,$map2_latin2;
$url = str_replace($map_latin2,$map2_latin2,$url);
return $url;
} |
function specialchar_latin2($url) {//specialchar_latin2 by tosiek https://tosiek.pl/ global $map_latin2,$map2_latin2; $url = str_replace($map_latin2,$map2_latin2,$url); return $url;}
Należy pamiętać by tablice ze znakami narodowymi były zdefiniowane poza – przed funkcją. Ciąg zażółć?gęślą !?jaźń;;\ZAŻÓŁĆ []GĘŚLĄ (JAŹŃ--
po przejściu przez tę funkcję będzie miał postać: zazolc?gesla !?jazn;;\ZAZOLC []GESLA (JAZN–
, nie nadaje się do wykorzystania w adresie.
Jak na razie funkcja zamienia tylko znaki narodowe z tablic, dodajmy do niej wyrażenie regularne zamieniające wszystko co nie jest znakiem wchodzącym w skład słów i występujące jeden raz lub więcej na myślnik – (kto woli podkreślnik _), posłużymy się zdefiniowaną klasą negującą znaki wchodzące w skład słów \W
function specialchar_latin2($url) {
//specialchar_latin2 by tosiek https://tosiek.pl/
global $map_latin2,$map2_latin2;
$url = str_replace($map_latin2,$map2_latin2,$url);
$url = preg_replace('#[\W]{1,}#','-',$url);
return $url;
} |
function specialchar_latin2($url) {//specialchar_latin2 by tosiek https://tosiek.pl/ global $map_latin2,$map2_latin2; $url = str_replace($map_latin2,$map2_latin2,$url); $url = preg_replace('#[\W]{1,}#','-',$url); return $url;}
Teraz zamieniamy wszystko co zbędne (spacje, wykrzykniki, znaki zapytanie i tym podobne na myślniki. Ciąg zażółć?gęślą !?jaźń;;\ZAŻÓŁĆ []GĘŚLĄ (JAŹŃ--
po przejściu przez funkcję miałby postać: zazolc-gesla-jazn-ZAZOLC-GESLA-JAZN-
, lecz jak widać pozostał myślnik na końcu, może także czasami pozostać na początku co jest całkowicie zbędne, użyjemy funkcji trim() do jego usunięcia:
function specialchar_latin2($url) {
//specialchar_latin2 by tosiek https://tosiek.pl/
global $map_latin2,$map2_latin2;
$url = str_replace($map_latin2,$map2_latin2,$url);
$url = preg_replace('#[\W]{1,}#','-',$url);
$url = trim($url,'-');
return $url;
} |
function specialchar_latin2($url) {//specialchar_latin2 by tosiek https://tosiek.pl/ global $map_latin2,$map2_latin2; $url = str_replace($map_latin2,$map2_latin2,$url); $url = preg_replace('#[\W]{1,}#','-',$url); $url = trim($url,'-'); return $url;}
Teraz ciąg miałby postać: zazolc-gesla-jazn-ZAZOLC-GESLA-JAZN
, a co jeśli byśmy chcieli by składał się z małych estetycznych liter? Wystarczy skorzystać z funkcji strtolower() zamieniającej wielkie litery na małe.
function specialchar_latin2($url) {
//specialchar_latin2 by tosiek https://tosiek.pl/
global $map_latin2,$map2_latin2;
$url = str_replace($map_latin2,$map2_latin2,$url);
$url = preg_replace('#[\W]{1,}#','-',$url);
$url = trim($url,'-');
$url = strtolower($url);
return $url;
} |
function specialchar_latin2($url) {//specialchar_latin2 by tosiek https://tosiek.pl/ global $map_latin2,$map2_latin2; $url = str_replace($map_latin2,$map2_latin2,$url); $url = preg_replace('#[\W]{1,}#','-',$url); $url = trim($url,'-'); $url = strtolower($url); return $url;}
Teraz otrzymujemy oczekiwany efekt zazolc-gesla-jazn-zazolc-gesla-jazn
, zamienione znaki narodowe na ich odpowiedniki, spacje i inne znaki specjalne zamienione na myślniki (lub podkreślniki) oraz cały ciąg pisany mała czcionką bez wielkich liter. Taki ciąg można wykorzystać w adresie URL.
Bardzo przydatna rzecz do tworzenia przyjaznych adresów ze słowami kluczowymi. Oczywiście dla kodowania iso-8859-2, które niestety nadal jest w użyciu.
Funkcja specialchar_latin2() w ostatecznej wersji:
$map_latin2=array(
"\xa1","\xa3","\xa5","\xa6","\xa7","\xa9","\xaa","\xab","\xac","\xae","\xaf",
"\xb1","\xb3","\xb5","\xb6","\xb9","\xba","\xbb","\xbc","\xbe","\xbf","\xc0",
"\xc1","\xc2","\xc3","\xc4","\xc5","\xc6","\xc7","\xc8","\xc9","\xca","\xcb",
"\xcc","\xcd","\xce","\xcf","\xd0","\xd1","\xd2","\xd3","\xd4","\xd5","\xd6",
"\xd7","\xd8","\xd9","\xda","\xdb","\xdc","\xdd","\xde","\xdf","\xe0","\xe1",
"\xe2","\xe3","\xe4","\xe5","\xe6","\xe7","\xe8","\xe9","\xea","\xeb","\xec",
"\xed","\xee","\xef","\xf0","\xf1","\xf2","\xf3","\xf4","\xf5","\xf6","\xf8",
"\xf9","\xfa","\xfb","\xfc","\xfd","\xfe",
);
//Tablice znakow diakrytycznych
//dla kodowania iso-8859-2 (latin2)
//by tosiek - https://tosiek.pl/
$map2_latin2=array(
"\x41","\x4c","\x4c","\x53","\x53","\x53","\x53","\x54","\x5a","\x5a","\x5a",
"\x61","\x6c","\x6c","\x73","\x73","\x73","\x74","\x7a","\x7a","\x7a","\x52",
"\x41","\x41","\x41","\x41","\x4c","\x43","\x43","\x43","\x45","\x45","\x45",
"\x45","\x49","\x49","\x44","\x44","\x4e","\x4e","\x4f","\x4f","\x4f","\x4f",
"\x78","\x52","\x55","\x55","\x55","\x55","\x59","\x54","\x73\x73","\x72","\x61",
"\x61","\x61","\x61","\x6c","\x63","\x63","\x63","\x65","\x65","\x65","\x65",
"\x69","\x69","\x64","\x64","\x6e","\x6e","\x6f","\x6f","\x6f","\x6f","\x72",
"\x75","\x75","\x75","\x75","\x79","\x74",
);
function specialchar_latin2($url) {
//specialchar_latin2 by tosiek https://tosiek.pl/
global $map_latin2,$map2_latin2;
$url = str_replace($map_latin2,$map2_latin2,$url);
$url = preg_replace('#[\W]{1,}#','-',$url);
$url = trim($url,'-');
$url = strtolower($url);
return $url;
} |
$map_latin2=array( "\xa1","\xa3","\xa5","\xa6","\xa7","\xa9","\xaa","\xab","\xac","\xae","\xaf", "\xb1","\xb3","\xb5","\xb6","\xb9","\xba","\xbb","\xbc","\xbe","\xbf","\xc0", "\xc1","\xc2","\xc3","\xc4","\xc5","\xc6","\xc7","\xc8","\xc9","\xca","\xcb", "\xcc","\xcd","\xce","\xcf","\xd0","\xd1","\xd2","\xd3","\xd4","\xd5","\xd6", "\xd7","\xd8","\xd9","\xda","\xdb","\xdc","\xdd","\xde","\xdf","\xe0","\xe1", "\xe2","\xe3","\xe4","\xe5","\xe6","\xe7","\xe8","\xe9","\xea","\xeb","\xec", "\xed","\xee","\xef","\xf0","\xf1","\xf2","\xf3","\xf4","\xf5","\xf6","\xf8", "\xf9","\xfa","\xfb","\xfc","\xfd","\xfe",);//Tablice znakow diakrytycznych //dla kodowania iso-8859-2 (latin2)//by tosiek - https://tosiek.pl/$map2_latin2=array( "\x41","\x4c","\x4c","\x53","\x53","\x53","\x53","\x54","\x5a","\x5a","\x5a", "\x61","\x6c","\x6c","\x73","\x73","\x73","\x74","\x7a","\x7a","\x7a","\x52", "\x41","\x41","\x41","\x41","\x4c","\x43","\x43","\x43","\x45","\x45","\x45", "\x45","\x49","\x49","\x44","\x44","\x4e","\x4e","\x4f","\x4f","\x4f","\x4f", "\x78","\x52","\x55","\x55","\x55","\x55","\x59","\x54","\x73\x73","\x72","\x61", "\x61","\x61","\x61","\x6c","\x63","\x63","\x63","\x65","\x65","\x65","\x65", "\x69","\x69","\x64","\x64","\x6e","\x6e","\x6f","\x6f","\x6f","\x6f","\x72", "\x75","\x75","\x75","\x75","\x79","\x74",);function specialchar_latin2($url) {//specialchar_latin2 by tosiek https://tosiek.pl/ global $map_latin2,$map2_latin2; $url = str_replace($map_latin2,$map2_latin2,$url); $url = preg_replace('#[\W]{1,}#','-',$url); $url = trim($url,'-'); $url = strtolower($url); return $url;}
Życzę miłego tworzenia przyjaznych adresów dla wyszukiwarek i użytkowników (wiesz w co klikasz widząc sam link).
Najnowsze komentarze