Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal to SQL's %like% but in PHP

Tags:

sql

php

equals

Is there any function in PHP equal to SQL's %like%?

E.g.

$a = 'I drove the Car';
$b = 'i_drove_the_car';

if(like($a, $b))
{
 echo "The strings have an %like% match";
}
like image 809
JohnF Avatar asked Oct 22 '25 20:10

JohnF


2 Answers

Use strstr, or, if you want case insensitive, stristr:

if(strstr($a, $b)) {
    echo "$b is in $a";
}
like image 108
Michiel Avatar answered Oct 24 '25 10:10

Michiel


You can use regular expression, i.e. preg_match.

if (preg_match('#.*' . $b . '.*#/i', $a)) {
    echo 'smth';
}

Option i means case insensetive mathcing.

like image 39
Ivan Velichko Avatar answered Oct 24 '25 09:10

Ivan Velichko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!