我有一个包含字段first_name和last_name的db表。我需要检查一下由Asiapay提供的信用卡持有者的名字是否属于我的旅行聚会(记录在我的db表中)。问题是,给出的信用卡持有人是全名(单栏)。
到目前为止,我的选择是:
我需要一些关于字符串比较的其他库和策略的建议。
PS:我只是无法比较连接的first_name和last_name与holder_name,因为有些情况下旅行文件中的名字与个人信用卡中的名字不同(例如。"John“(护照中的与信用卡中的"John”)
发布于 2019-02-22 07:37:55
下面是您应该如何实现全文搜索
首先,在您的表上创建全文搜索。
// Full Text Index
DB::statement('ALTER TABLE contacts ADD FULLTEXT fulltext_index (firstName, lastName, email)');
然后在你的模型里
$columns = 'firstName,lastName,email';
$contacts = Contact::where('customer_id', $request->user()->customer_id)
->select(['id', 'firstName', 'lastName', 'email', 'phone', 'mobile', 'lastContacted', 'assignedTo', 'createdBy'])
->whereRaw("MATCH ({$columns}) AGAINST (? IN BOOLEAN MODE)", $this->fullTextWildcards($q))
->paginate(10);
下面是函数fullTextWildcards
,它用全文搜索通配符替换空格。
protected function fullTextWildcards($term)
{
// removing symbols used by MySQL
$reservedSymbols = ['-', '+', '<', '>', '@', '(', ')', '~'];
$term = str_replace($reservedSymbols, '', $term);
$words = explode(' ', $term);
foreach ($words as $key => $word) {
/*
* applying + operator (required word) only big words
* because smaller ones are not indexed by mysql
*/
if (strlen($word) >= 3) {
$words[$key] = '*' . $word . '*';
}
}
$searchTerm = implode(' ', $words);
return $searchTerm;
}
发布于 2019-12-18 09:42:31
看看这个包裹。有许多选项可以改进全文搜索。
发布于 2018-10-17 10:12:21
您可能会用空格将字符串炸开:
$name = explode(' ', $name);
然后得到名字:
$first = array_shift($name);
那就知道姓:
$last = array_pop($name);
那么剩下的应该是相当琐碎的。祝好运!
https://stackoverflow.com/questions/52852264
复制相似问题