CodeIgniter Forums
Query builder - like() with wildcard within a string - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Query builder - like() with wildcard within a string (/showthread.php?tid=66034)



Query builder - like() with wildcard within a string - p4xel - 08-26-2016

Hi,

I know I can use wildcards on the beginning and end of the string, but I can't figure out how (or if) I can use it withing the string.

I know how $this->db->like() works:
PHP Code:
$this->db->like('title''match');  //produces WHERE `title`LIKE '%match%' 

but sometimes I need to match just one charachter with "_" withing the match string or something more custom
PHP Code:
WHERE `titleLIKE 'ma_ch'
WHERE `titleLIKE 'ma%h%' 

Thanks


RE: Query builder - like() with wildcard within a string - Wouter60 - 08-27-2016

You can use the MySQL function REGEXP to check for a pattern.
For example (not tested, but it gives you the right idea):
PHP Code:
WHERE title REGEXP 'ma*h*' 

More info: http://dev.mysql.com/doc/refman/5.7/en/regexp.html


RE: Query builder - like() with wildcard within a string - JayAdra - 08-27-2016

http://www.codeigniter.com/userguide3/database/query_builder.html#looking-for-specific-data

Number 4. Try writing the custom SQL you need using that.


RE: Query builder - like() with wildcard within a string - p4xel - 08-27-2016

I was hoping there is an extra argument (like 'before', 'after', 'both') which could allow to use a wildcards within the string.
I like using simple query builder syntax, but in this case I will make my own where() query. I don't need any fancy stuff so won't use Regex (at least on this occasion).

Thanks for help.


RE: Query builder - like() with wildcard within a string - kenjis - 08-28-2016

Probably you need like this:


PHP Code:
$this->db->like('title''ma_ch''raw'); 

Why don't you send a feature request to http://forum.codeigniter.com/forum-17.html ?