Php – Analog of MySQL like function


Suppose it's a nub question, but is there an analog of MySQL's LIKE function in PHP?

So, e.g.

like('goo*','');//is true
like('','')//also true

I know regex rullez, but don't know any solution to reach this

Best Solution

For the first, use strpos:

like('goo*','');      -->  strpos('goo','') === 0

The next one, you can use strpos:

like('*gl*','');      -->  strpos('gl', '') !== false;

The next you can just use equals:

like('','') -->  '' == ''

Of course, you can use regex for all of them:

like('goo*','');      -->  preg_match('#^goo.*$#','') 
like('*gl*','');      -->  preg_match('#^.*gl.*$#', '');
like('','') -->  preg_match('#^google\.com$#', '')

Edit: to convert your patterns to regex, place a ^ at the beginning, and a $ at the end, then replace * with .* and escape .s.