【问题标题】:How do you ignore dashes and commas in a php search bar你如何忽略php搜索栏中的破折号和逗号
【发布时间】:2019-09-12 01:59:15
【问题描述】:

目前,在我的搜索栏中,如果我完全按照输入数据库的方式输入城市名称,我就能找到它。例如:Nuits-Saint-Georges 或北卡罗来纳州达勒姆。

我希望我的用户能够输入 Nuits Saint Georges 或 Durham NC。

城市位于“位置”。

function Wo_GetAllJobs($filter_data = array()) {
global $wo, $sqlConnect;
$data = array();
$query_one = " SELECT * FROM " . T_JOB . " WHERE status = '1'";
if (!empty($filter_data['c_id'])) {
$category = $filter_data['c_id'];
$query_one .= " AND `category` = '{$category}'";
}
if (!empty($filter_data['after_id'])) {
if (is_numeric($filter_data['after_id'])) {
$after_id = Wo_Secure($filter_data['after_id']);
$query_one .= " AND `id` < '{$after_id}' AND `id` <> $after_id";
}
}
if (!empty($filter_data['keyword'])) {
$keyword = Wo_Secure($filter_data['keyword']);
$query_one .= " AND (`title` LIKE '%{$keyword}%' OR `location` LIKE 
'%{$keyword}%') ";
}
if (!empty($filter_data['user_id'])) {
$user_id = Wo_Secure($filter_data['user_id']);
$query_one .= " AND `user_id` = '{$user_id}'";
}
if (!empty($filter_data['type'])) {
$type = Wo_Secure($filter_data['type']);
$query_one .= " AND `job_type` = '{$type}'";
}

if (!empty($filter_data['length'])) {
$user_lat = $wo['user']['lat'];
$user_lng = $wo['user']['lng'];
$unit = 6371;
$query_one = " AND status = '1'";
$distance = Wo_Secure($filter_data['length']);
if (!empty($filter_data['c_id'])) {
$category = $filter_data['c_id'];
$query_one .= " AND `category` = '{$category}'";
}
if (!empty($filter_data['after_id'])) {
if (is_numeric($filter_data['after_id'])) {
$after_id = Wo_Secure($filter_data['after_id']);
$query_one .= " AND `id` < '{$after_id}' AND `id` <> $after_id";
}
}
if (!empty($filter_data['keyword'])) {
$keyword = Wo_Secure($filter_data['keyword']);
$query_one .= " AND (`title` LIKE '%{$keyword}%' OR `location` LIKE 
'%{$keyword}%') ";
}
if (!empty($filter_data['user_id'])) {
$user_id = Wo_Secure($filter_data['user_id']);
$query_one .= " AND `user_id` = '{$user_id}'";
}

if (!empty($filter_data['type'])) {
$type = Wo_Secure($filter_data['type']);
$query_one .= " AND `job_type` = '{$type}'";
}

【问题讨论】:

  • 我不认为你的整个函数都在这里列出。
  • 用你的数据库标记你的问题,是 MySQL 吗?

标签: php mysql


【解决方案1】:

用空格分割你的关键字,然后用%将它们内爆

//TODO query_one
//$query_one .= " AND (`title` LIKE ? OR `location` LIKE ?) ";
$stmt = $mysqli->prepare($query_one);
$keyWord = "%" . implode("%", explode(" ", $keyword)) . "%";
$stmt->bind_param("s", $keyWord));
$stmt->bind_param("s", $keyWord));
$stmt->execute();

请记住,准备好的语句是必须并且易于switch to

注意:

在关键字开头使用%会使优化器无法使用该列上的索引,如果有的话,所以如果你的数据量很大并且你开始遇到性能问题,你可以去到全文搜索(FTS)检查this

不用担心 MySQL 上的区分大小写比较

而关于MySQL会进行比较的情况,我会引用手册"Case Sensitivity in String Searches"

对于非二进制字符串(CHAR、VARCHAR、TEXT),字符串搜索使用 比较操作数的排序规则。对于二进制字符串(BINARY, VARBINARY, BLOB),比较使用中字节的数值 操作数;这意味着对于字母字符,比较 将区分大小写。

..

默认字符集和排序规则为 utf8mb4 和 utf8mb4_0900_ai_ci,所以默认情况下非二进制字符串比较不区分大小写。

【讨论】:

    【解决方案2】:

    这就是我最终解决此问题的方法。

    if (!empty($filter_data['keyword'])) {
        $keyword = $filter_data['keyword'];
        $keywordSearch = Wo_Secure(str_replace(',','',str_replace('-','',str_replace('.','',str_replace(' ', "", $keyword)))));
        $keyword = Wo_Secure($keyword);
        $query_one .= " AND (`title` LIKE '%{$keyword}%' OR REPLACE(REPLACE(REPLACE(REPLACE(`location`,' ',''),'-',''),'.',''),',','')LIKE '%{$keywordSearch}%') ";
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-07
      • 1970-01-01
      • 2011-05-31
      • 2022-01-10
      • 1970-01-01
      • 2020-02-21
      • 2020-07-01
      • 1970-01-01
      • 2020-04-26
      相关资源
      最近更新 更多