我正在使用带有 PHP 驱动程序的 MongoDB,并且正在使用土耳其语字符集。但是当我尝试匹配土耳其字符时,出现了问题。
例如,我的数据库包含以下值:
- 英语
- 意大利
- 桑皮约纳
当我在数据库中搜索它们时,没有结果。
$database->find(array('value'=>'İngiltere'));
给我空的结果,因为它不匹配土耳其字符,它不自动匹配。
可以做些什么来解决这个问题?
我正在使用带有 PHP 驱动程序的 MongoDB,并且正在使用土耳其语字符集。但是当我尝试匹配土耳其字符时,出现了问题。
例如,我的数据库包含以下值:
当我在数据库中搜索它们时,没有结果。
$database->find(array('value'=>'İngiltere'));
给我空的结果,因为它不匹配土耳其字符,它不自动匹配。
可以做些什么来解决这个问题?
$database->find(array('value'=>'İngiltere'));
There is no find()
method on the MongoDB class. Is $database
a MongoCollection instance?
You may be running into trouble with UTF-8 encoding (see: this answer for related discussion), but all three of the strings you shared appear to be properly encoded. Using UTF-8 strings, there should be no issue inserting and querying with said values. Consider the following code example:
$values = ['İngiltere', 'İtalya', 'Şampiyona'];
$m = new MongoClient();
$c = $m->test->foo;
$c->drop();
foreach ($values as $value) {
printf("Testing value: %s\n", $value);
printf("Detected encoding: %s\n", mb_detect_encoding($value));
$c->insert(['x' => $value]);
var_dump($c->findOne(['x' => $value]));
echo "\n";
}
This generates the following output on my system:
Testing value: İngiltere
Detected encoding: UTF-8
array(2) {
'_id' =>
class MongoId#6 (1) {
public $$id =>
string(24) "5213d027e84df17727ca7985"
}
'x' =>
string(10) "İngiltere"
}
Testing value: İtalya
Detected encoding: UTF-8
array(2) {
'_id' =>
class MongoId#5 (1) {
public $$id =>
string(24) "5213d027e84df17727ca7986"
}
'x' =>
string(7) "İtalya"
}
Testing value: Şampiyona
Detected encoding: UTF-8
array(2) {
'_id' =>
class MongoId#4 (1) {
public $$id =>
string(24) "5213d027e84df17727ca7987"
}
'x' =>
string(10) "Şampiyona"
}