drutcs7210 2016-10-13 00:06
浏览 51

基于值preg_match将不同的“胶水”插入到内爆函数中

I'm performing a search query with MATCH and AGAINST in Bolean Mode, and i have the keywords stored in an ARRAY to show they like a TAGS, when I IMPLODE the array values to construct the query statement it works fine, but i want to preserve the BOLEAN functionality search like "exact match" or other special characters.

Is it possible to insert quotes when the array value comes with a spaces?

INPUT: keyword1 keyword2 "exact match" keyword3

Covert to this array:
$keywords = array(
    [0] = "keyword1",
    [1] = "keyword2",
    [2] = "exact match", <-- This came with quotes so is stored as a phrase
    [3] = "keyword3"
);

Then i need to convert to SQL statement with implode:

$SQL  = "SELEC field WHERE MATCH(keywords) AGAINST('";
$SQL .= implode(" ", $keywords);
$SQL .= "' IN BOOLEAN MODE)";

I Try to use ARRAY_FILTER() but it only discriminates one instead the other:

function words($var){
    return preg_match('/\s/', $var);
}

$SQL  = "SELEC field WHERE MATCH(keywords) AGAINST('";
$SQL .= implode(" ", array_filter($keywords, 'words'));
$SQL .= "' IN BOOLEAN MODE)";

//example above only outputs: exact match

My goal is to discriminates array value when the value comes with more than two words, the function adds quotes to the value

Expected Output:

$SQL = "SELECT field WHERE MATCH(keywords) AGAINST('keyword1 keyword2 "exact match" keyword3' IN BOOLEAN MODE)";

Thanks a lot for your help

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)