Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

php - Custom REGEXP Function to be used in a SQLITE SELECT Statement

I have an SQLITE Database's File which in one of the table columns there is some simple Regular Expressions.

These Expressions are something like /foo(.?) or /foo/bar/(.?) and so on...

Well, when we try to match some text against a Regular Pattern, in PHP, we do:

preg_match( $pattern, $target, $matches )

Replacing the variables with the content, obviously.

What I would like to do is send ANY STRING as value of a WHERE Clause and, when searching the SQLITE Database's File, use each of the stored Regular Expressions to match a pattern in the given string.

I think that using PHP's sqlite_create_function() I can create some kind of routine to do this, but I don't know exactly how, since is the first time I develop using SQLITE.

If interest, it's part of an MVC Routing of a Framework I'm developing.

Thank you very much, in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use SQLiteDatabase::createFunction documentation here or PDO::sqliteCreateFunction documentation here

I did something like this:

<?php
function _sqliteRegexp($string, $pattern) {
    if(preg_match('/^'.$pattern.'$/i', $string)) {
        return true;
    }
    return false;
}
$PDO->sqliteCreateFunction('regexp', '_sqliteRegexp', 2);
?>

Use:

SELECT route FROM routes WHERE pattern REGEXP 'your/url/string' LIMIT 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...