Czasem człowiek (czytaj developer) potrzebuje pozbyć się znaków narodowych i wysłać/zapisać gdzieś tekst bez polskich znaków. Tym którzy piszą w Zend Framework przychodzi mój mały filtr.
Jak każdego filtra w Zend Framework-u :)
$asciiFilter = new Zend_Filter_Ascii();
echo $asciiFilter->filter('Zażółć gęślą jaźń'); // Zazolc gesla jazn
Jak to działa można zobaczyć na demonstracji http://smoku.net/artykuly/zend-filter-ascii-demo
require_once 'Zend/Filter/Interface.php';
/**
* konweruje striing z UTF-8 na ASCII
*
*/
class Zend_Filter_Ascii implements Zend_Filter_Interface
{
public function filter($value)
{
$value = strtr($value, '`\'"^~', '-----');
if (ICONV_IMPL === 'glibc') {
$value = iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $value);
$value = strtr($value,
"\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",
"ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYT"
."sraaaalccceeeeiiddnnooooruuuuyt");
} else {
$value = iconv('UTF-8', 'ASCII//TRANSLIT', $value);
}
$value = str_replace(array('`', "'", '"', '^', '~'), '', $value);
return $value;
}
}