【问题标题】:How to filter keyword for integer, string and integer, string with dashes and integer based on keyword如何根据关键字过滤整数,字符串和整数,带有破折号的字符串和整数的关键字
【发布时间】:2015-05-21 01:50:11
【问题描述】:

我已经实现了搜索,其工作是从数据库中找出属性记录。搜索在某种程度上完全取决于用户输入的关键字。

假设,

如果用户键入 12344775773 之类的关键字,则应从数据库中的 zip 列中搜索记录。

接下来,如果用户键入 6 S Surfway70 N Grove St1661-114 Old Country Rd 之类的关键字,则应仅从数据库中搜索 address 列中的记录。

接下来,如果用户键入 CalvertonFreeportRiverhead 之类的关键字,则应仅从数据库中搜索 town 列中的记录。

所以,我的问题是我无法根据用户输入的关键字过滤这些记录。我的问题是从所需列中过滤基于关键字的记录。

这是我正在使用的代码:

<?php
header('Content-Type: application/json');

$key        = $_REQUEST['keyword'];
$searchOp   = $_REQUEST['searchOp'];

if($searchOp==1 || $searchOp==6)
{
        $searchChk = "(`property_chk` = '1' OR `property_chk` = '6')";
    }else{
        $searchChk = "(`property_chk` = '$searchOp')";
    }

$data = array(
              'locations' => array(),
              'errors'=>array(),
              'success'=> true,
              );

if($db->connect_errno > 0){
    $data['errors'][] = $db->connect_error;
    die(json_encode($data));
}

$index = 0;

if(intval($key) >= 3){

$zipQuery = $db->query("SELECT zip FROM tbl_property WHERE zip like '%$key%' AND $searchChk GROUP BY zip LIMIT 0,5");
if($zipQuery->num_rows>0){
    while($row = $zipQuery->fetch_assoc()){
    $data['locations'][] = array(
        "value"=> $row['zip'],
        "altValue"=> null,
        "display"=> $row['zip'],
        "type"=> "zip",
        "propertyIndex"=> "",
        "index"=> $index
    );
    $index = $index + 1;
        }
    }

}elseif (!intval($key) && count($key) > 4){

$cityQuery = $db->query("SELECT ste,town FROM tbl_property WHERE town like '%$key%' AND $searchChk GROUP BY town LIMIT 0,5");
if($cityQuery->num_rows>0){
    while($row = $cityQuery->fetch_assoc()){
    $data['locations'][] = array(
        "value"=> $row['town'].', '.$row['ste'],
        "altValue"=> null,
        "display"=> $row['town'].', '.$row['ste'],
        "type"=> "city",
        "propertyIndex"=> "",
        "index"=> $index
    );
    $index = $index + 1;
        }
    }

}elseif (count($key) > 1){

$addrQuery = $db->query("SELECT addr FROM tbl_property WHERE addr like '%$key%' AND $searchChk GROUP BY addr LIMIT 0,5");
if($addrQuery->num_rows>0){
    while($row = $addrQuery->fetch_assoc()){
    $data['locations'][] = array(
        "value"=> $row['addr'],
        "altValue"=> null,
        "display"=> $row['addr'],
        "type"=> "address",
        "propertyIndex"=> "",
        "index"=> $index
    );
    $index = $index + 1;
        }
    }
}
$db->close();

header('Content-Type: application/json');
echo json_encode($data);

【问题讨论】:

    标签: php validation mysqli filtering keyword-search


    【解决方案1】:

    您可以通过一些正则表达式测试来实现这一点。假设你被规定的规则是那么严格。

    $matches = array(); // We do not use it but has to be defined
    if (preg_match("/^([a-zA-Z ])+$/", $key, $matches)) {
    
        // Do search by city here
    
    } else if (preg_match("/^([0-9])+$/", $key, $matches)) {
    
        // Do search by postal code here
    
    } else if (preg_match("/^([a-zA-Z])*([0-9 -])+([a-zA-Z \.])+$/", $key, $matches)) {
    
        // Do search by address here
    
    }
    

    正则表达式小提琴示例:

    邮政 - https://regex101.com/r/qK8pL7/2

    城市 - https://regex101.com/r/yS1oF1/2

    地址 - https://regex101.com/r/pZ7dT3/2

    这些示例假定邮政编码始终是数字并且不包含任何其他字符。假定城市始终是字母字符和/或空格,任何其他字符都会导致匹配失败。

    使用小提琴更新您希望允许的其他可能的字符,以便更适合您的需求

    除了打开else 语句之外,您还可以为地址创建一个正则表达式。不过这会有点棘手,可能不适用

    【讨论】:

    • 我们可以试试regexaddress 吗?
    • 您犯了一些错误,先生。对于postal,它应该是:} else if (preg_match("/^([0-9])*$/", $key, $matches)) {,对于city,它应该是:if (preg_match("/^([a-zA-Z ])*$/", $key, $matches)) {
    • 是的,对不起,我交换了正则表达式。但是这个概念对你有用吗?会更新答案
    • 越来越近了,先生。我也可以address 吗?
    • 给定的格式是唯一可能的不同类型?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多