2016-04-07 34 views
0

我有一些punycodesmongoDB。我需要用他們的母語對用戶請求進行搜索。如果用戶輸入了完整的地址,我可以找到它,但如果他輸入了地址的一部分,那麼我找不到它們。這是不是一個真正的代碼,但我做這樣的事情:在punycode中搜索

//$punycode = 'xn--tst-qla.de'; //täst.de 

$query1 = 'tä'; 
$query2 = 'täst'; 

$queryPunicode1 = Heplper::punycodeEncode($query1); //xn--t-0fa 
$queryPunicode2 = Heplper::punycodeEncode($query2); //xn--tst-qla.de 

$condition1 = ['ref_host' => ['$regex' => queryPunicode1],]; 
$result1 = CwShow::findAggregateSum($condition1); // false 

$condition2 = ['ref_host' => ['$regex' => queryPunicode2],]; 
$result2 = CwShow::findAggregateSum($condition2); // true 

任何試圖找到$query1$punycode回報false。如何在$punycode中找到$query1

回答

0

我在數據庫中添加了一個新字段,該字段以本地語言存儲網址。而在服務器端我選擇哪個字段做搜索

$query = 'tä'; 
$punycode = Heplper::punycodeEncode($query); 
$field = Heplper::isPunycode($punycode) ? 'ref_host_native' : 'ref_host'; 
$condition = [$field => ['$regex' => $query],]; 
$result = CwShow::findAggregateSum($condition);