PHP way to do SQL LIKE matching without querying the database?

I want to combine the input string with my PHP page in the same way as matching the LIKE command in SQL (MySQL) for consistency of other searches. Because (I saw, but don't understand), some PHP syntaxes include SQL commands. I wonder if this is possible?

The reason for this is that now I am performing a search on keywords and fields in the database that are stored in a serialized array, which I have to perform for non-serialization in PHP and search depending on the structure of the array. I cannot query the table, I just need the corresponding query ability. Otherwise, I need to find an alternative routine that will not be consistent. I cannot go back and restructure the database, as this was not expected when you return to the specification. Yes, I need an ugly hack, but I'm looking for the most elegant one.

If this is not possible, I could use any recommendation for matching custom text as a keyword from the saved text.

EDIT (for clarification): my main problem is that I do not have a full understanding of how the LIKE command works (just copying the code), and since the keyword implies some degree of uncertainty, I would like this uncertainty to persist if I switch to regex. I'm better with regex just not very good with like. My query: "LIKE" matchme% '"

+3
source share
4 answers

Update

Based on tomalak's comment and OIS’s brilliant idea for using preg_grep, this could be something more that matches the 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

, ?

<?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>';
}
+7

preg_grep.

$arr = array("tstet", "duh", "str");
$res = preg_grep("#st#i", $arr); //i for case insensitive
var_dump($res);

array(2) {
  [0]=>
  string(5) "tstet"
  [2]=>
  string(3) "str"
}

:

, . %. LIKE 'text%'

regex

"#st#i"  regex is the same as in sql "%st%"
"#^st#i" regex is the same as in sql "st%"
"#st$#i" regex is the same as in sql "%st"

, preg_quote , . $ regex = "#". preg_quote ($ text). "#"; $ res = preg_grep ($ regex, $arr);

+4

, preg_match, , LIKE.

<?php // The "i" after the pattern delimiter indicates a case-insensitive search 
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found."; 
} else {
    echo "A match was not found."; } 
?>
0

, , LIKE var%?

strpos (haystack, needle) % var%.

if( strpos($source, "var") == 0 ) echo "matches var%";
if( strlen($source) - (strpos($source, "var")) == strlen("var") ) echo "matches %var";

. , , .

0

Source: https://habr.com/ru/post/1703463/


All Articles