Created
January 3, 2013 05:54
-
-
Save wingsline/4441139 to your computer and use it in GitHub Desktop.
Laravel: SQLite REGEXP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create the function | |
DB::connection()->pdo->sqliteCreateFunction("REGEXP", "preg_match", 2); | |
// build a query | |
DB::table('tablename')->raw_where('REGEXP("#^[a-z]+$#iu", tablename.row)')->get(); |
Thanks!
If you're using both SQLITE and MySQL
DB::connection()->getPdo()->sqliteCreateFunction('REGEXP', function ($pattern, $value) {
mb_regex_encoding('UTF-8');
return (false !== mb_ereg("/$pattern/", $value)) ? 1 : 0;
});
is a good alternative;
Thanks all for this thread 👏
I had to remove the delimiter to make this work. I use this code in my AppServiceProvider.
if (DB::Connection() instanceof \Illuminate\Database\SQLiteConnection) {
DB::connection()->getPdo()->sqliteCreateFunction('REGEXP', function ($pattern, $value) {
mb_regex_encoding('UTF-8');
return (false !== mb_ereg($pattern, $value)) ? 1 : 0;
});
}
Thanks! Works perfectly!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this great line!
I don't know for older versions, but as for L5, you should go this way:
since DB wouldn't allow you to access 'pdo' as it's a private property now.
see http://laravel.com/docs/4.2/database#accessing-connections