PHP的方式来执行SQL LIKE匹配没有数据库查询? [英] PHP way to execute SQL LIKE matching without a database query?

查看:108
本文介绍了PHP的方式来执行SQL LIKE匹配没有数据库查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将输入字符串匹配到我的PHP页面,就像SQL(MySQL)中的LIKE命令所做的匹配一样,以实现其他搜索的一致性。既然(我已经看到但不理解),一些PHP语法包括SQL命令,我想知道这是否可能?

I want to match an input string to my PHP page the same way a match done by the LIKE command in SQL (MySQL) for consistency of other searches. Since (I have seen but don't comprehend) some of the PHP syntax includes SQL commands I am wondering if this is possible?

原因是我现在实现一个关键字与数据库中存储在序列化数组中的字段的搜索,我必须根据数组结构在PHP中进行反序列化并进行搜索。我无法查询表格,只需要查询的匹配能力。否则,我需要找到一个替代匹配例程,这将不一致。我不能回去重新构建数据库,因为这在规范中没有预料到。是的,我需要一个丑陋的黑客,但我正在寻找最优雅的。

The reason for this is I am now implementing a search of a keyword versus fields in the DB that are stored in a serialized array, which I have to unserialize in PHP and search depending on the structure of the array. I can't query against the table, just need the matching ability of the query. Otherwise I need to find an alternate matching routine, which won't be consistent. I can't go back and re-structure the DB since this wasn't anticipated wayyy back in the spec. Yes, I need an ugly hack, but am looking for the most elegant.

如果这是不可能的,我可以使用任何建议将用户输入的文本作为关键字与存储文本。

If it's not possible I could use any recommendation of matching user typed text as a keyword against stored text.

编辑(澄清):我的主要问题是我没有彻底掌握LIKE命令的工作原理(只是复制代码)关键字意味着某种程度的模糊性,如果我切换到正则表达式,我希望保留这种模糊性。与正则表达式相比,我更擅长使用正则表达式。我的查询是LIKE'matchme%'

EDIT (for clarification): my main problem is I don't have a thorough grasp on how the LIKE command works (just copying the code) and as the keyword implies some degree of vagueness, I would like that vagueness preserved if I switch to a regular expression. I am better with regex's just not so good with like. My query is "LIKE 'matchme%'"

推荐答案

更新



基于tomalak的评论和OIS使用preg_grep的绝妙想法,这可能更符合您最终解决方案的问题。

Update

Based on tomalak's comment and OIS's brilliant idea to use preg_grep, this might be something more along the lines of a final solution for you.

<?php

function convertLikeToRegex( $command )
{
    return "/^" . str_replace( '%', '(.*?)', preg_quote( $command ) ) .  "$/s";
}

function selectLikeMatches( $haystack, $needle )
{
    return preg_grep( convertLikeToRegex( $needle ), $haystack );
}

$likeClauses = array(
    '%foo'
    ,'foo%'
    ,'%foo%'
);

$testInput = array(
    'foobar'
    ,'barfoo'
    ,'barfoobaz'
);

foreach ( $likeClauses as $clause )
{
    echo "Testing $clause:";
    echo '<pre>';
    print_r( selectLikeMatches( $testInput, $clause ) );
    echo '</pre>';
}



原始文章



<

Original Post Below

Is this along the lines of what you're after?

<?php

function convertLikeToRegex( $command )
{
    return "/^" . str_replace( '%', '(.*?)', $command ) .  "$/s";
}

$likeClauses = array(
    '%foo'
    ,'foo%'
    ,'%foo%'
);

$testInput = array(
    'foobar'
    ,'barfoo'
    ,'barfoobaz'
);

foreach ( $testInput as $test )
{
    foreach ( $likeClauses as $clause )
    {
        echo "Testing '$test' against like('$clause'): ";
        if ( preg_match( convertLikeToRegex( $clause ), $test ) )
        {
            echo 'Matched!';
        } else {
            echo 'Not Matched!';
        }
        echo '<br>';
    }
    echo '<hr>';
}

这篇关于PHP的方式来执行SQL LIKE匹配没有数据库查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆