String: Odstranění diakritiky a zobecnění funkce webalize

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
knyttl
Člen | 196
+
+1
-

Ahoj,

myslím, že by bylo fajn do String přidat funkci na odstranění diakritiky – tu část dělá i webalize, ale ta má ale konkrétnější užití.

Tedy místo:

<?php
 public static function webalize($s, $charlist = NULL, $lower = TRUE) {
         $s = strtr($s, '`\'"^~', '-----');
         if (ICONV_IMPL === 'glibc') {
             $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); // intentionally @
             $s = strtr($s, "\xa5\xa3\xbc\x8c\xa7\x8a\xaa\x8d\x8f\x8e\xaf\xb9\xb3\xbe\x9c\x9a\xba\x9d\x9f\x9e\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",
                 "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
         } else {
             $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); // intentionally @
         }
         $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
         if ($lower) $s = strtolower($s);
         $s = preg_replace('#[^a-z0-9' . preg_quote($charlist, '#') . ']+#i', '-', $s);
         $s = trim($s, '-');
         return $s;
     }

?>

Mít něco jako:

<?php
    public static function removeDiacritics( $s ) {
         if (ICONV_IMPL === 'glibc') {
             $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); // intentionally @
             $s = strtr($s, "\xa5\xa3\xbc\x8c\xa7\x8a\xaa\x8d\x8f\x8e\xaf\xb9\xb3\xbe\x9c\x9a\xba\x9d\x9f\x9e\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",
                 "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
         } else {
             $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); // intentionally @
         }
	 return $s;
    }

    public static function webalize($s, $charlist = NULL, $lower = TRUE)
    {
        $s = strtr($s, '`\'"^~', '-----');
    	$s = self::removeDiacritics( $s );
        $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
        if ($lower) $s = strtolower($s);
        $s = preg_replace('#[^a-z0-9' . preg_quote($charlist, '#') . ']+#i', '-', $s);
        $s = trim($s, '-');
        return $s;
    }

?>

Editoval knyttr (17. 8. 2010 13:38)

knyttl
Člen | 196
+
0
-

Trochu jsem upřesnil můj návrh.

jtousek
Člen | 951
+
0
-

+1

btw. tohle patří do sekce Feature Requests

kravčo
Člen | 721
+
0
-

jtousek napsal(a):

btw. tohle patří do sekce Feature Requests

presunuté…

westrem
Člen | 398
+
0
-

+1 nedavno som si to pre vlasnut potrebu tak naimplementoval, bolo by pekne to mat uz default

David Grudl
Nette Core | 8147
+
0
-

Implementováno jako String::toAscii()