Мне нужно сравнить строки и сопоставить имена друг с другом, даже если они написаны не одинаково.
Например, DÉSIRÉ-Smith
должен соответствовать Desireesmith
, а также Desiree или Desi'ree Smith
. Поэтому у меня был следующий подход, который отлично работал в командной строке с использованием PHP -CLI:
class Alike {
static function convertAlike($string) {
// in case the first and last name or two first names are mixed up
$parts = preg_split('/[\s\-\.\_]/', $string, -1, PREG_SPLIT_NO_EMPTY);
sort($parts);
$string = implode($parts);
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string); // transliterate
$string = strtolower($string); // lowercase
$string = preg_replace('/[^a-z]/','',$string); // remove everything but a-z
$string = preg_replace('{(.)\1+}','$1',$string); // remove duplicate chars
return $string;
}
static function compareAlike($string1,$string2) {
return (strcmp(Alike::convertAlike($string1),Alike::convertAlike($string2)) === 0) ? true : false;
}
}
echo Alike::convertAlike("DÉSIRÉ-Smith").PHP_EOL; // desiresmith
echo Alike::convertAlike("Desireesmith").PHP_EOL; // desiresmith
echo Alike::convertAlike("Desi'ree Smith").PHP_EOL; // desiresmith
echo Alike::convertAlike("René Röyßeå likes special characters ½ € in ©").PHP_EOL; // reneroysealikespecialcharacterseurinc
var_dump(Alike::compareAlike("DÉSIRÉ-Smith","Desireesmith")); // true
var_dump(Alike::compareAlike("Desireesmith","Desi'ree Smith")); // true
var_dump(Alike::compareAlike("summer","winter")); // false
?>
Однако на моем веб-сайте работает Версия сервера: Apache / 2.2.14 (Ubuntu)
работает PHP Версия 5.3.2-1ubuntu4.2
как модуль, который я всегда получаю просто знаки вопроса.
The funny thing is that the error must occour in this line
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string); // transliterate
because afterwards i can see every character that has not been transliterated, but those that should have been replaced by ascii chars become question signs.
i tried every possible combination of input/output string encoding and iconv internal, input and output encoding settings as well as locale settings. i even did chmod -R 777 /usr/lib/gconv and moved the to my working dir.
however i saw this bug reported ont he mailing list: http://bugs.php.net/bug.php?id=44096
[2010-06-07 21:22 UTC] icovt at yahoo dot com
mod_php iconv() is not working properly if your apache is chrooted and you do not
have the content of /usr/lib/gconv/ folder into your relative chroot path (i.e.
/your/chroot/path/usr/lib/gconv/).
You can simply do:
cp /usr/lib/gconv/* /your/chroot/path/usr/lib/gconv/
... and re-try.
This was a fix for me, hope this could save time for somebody else.
P.S. Btw, initially iconv() called from command line (using php cli) was OK.
I tried that my www-data user is at home in /var/www/ and I ended up with the folder /var/www/usr/lib/gconv/ as well as /var/www/myproject/usr/lib/gconv/
FYI: I had encoding detection and transcoding functions to ensure the correct encodings to be passed, but removed them for the sake of clarity as they are not needed anwyay if you input utf8 strings everything should be fine...
Any ideas?
выяснил, что локаль была настроена неправильно, и мои попытки установить ее не увенчались успехом, поскольку локали, доступные в системе, на самом деле назывались иначе, чем в примерах справочной страницы (в соответствии с их кодировкой!)
простая locale -a
показала, что ;O)
setlocale(LC_ALL, "en_US.utf8");
это действительно сработало!
ну теперь эта функция работает отлично.
ну, теперь понятно, почему это работало и из консоли, потому что локаль была импортирована из текущих настроек оболочки пользователя ;) на самом деле просто нужно настроить любую локаль. на самом деле не имеет значения, какой именно, поскольку мы конвертируем в ascii, где все равны, только некоторые равнее других :)
Будьте осторожны, чтобы установить локаль, которая фактически установлена в вашей системе, и проверьте результат setlocale, потому что вы выиграли ничего не менять, если локаль не установлена или имя написано с ошибкой.