問題描述
我有一個(gè) SQLITE 數(shù)據(jù)庫文件,其中一個(gè)表列中有一些簡單的正則表達(dá)式.
I have an SQLITE Database's File which in one of the table columns there is some simple Regular Expressions.
這些表達(dá)式類似于 /foo(.?) 或 /foo/bar/(.?) 等等..
These Expressions are something like /foo(.?) or /foo/bar/(.?) and so on...
好吧,當(dāng)我們嘗試將一些文本與常規(guī)模式進(jìn)行匹配時(shí),在 PHP 中,我們會這樣做:
Well, when we try to match some text against a Regular Pattern, in PHP, we do:
preg_match( $pattern, $target, $matches )
preg_match( $pattern, $target, $matches )
顯然是用內(nèi)容替換變量.
Replacing the variables with the content, obviously.
我想做的是發(fā)送任何字符串作為 WHERE 子句的值,并在搜索 SQLITE 數(shù)據(jù)庫的文件時(shí),使用每個(gè)存儲的正則表達(dá)式來匹配給定字符串中的模式.
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.
我認(rèn)為使用 PHP 的 sqlite_create_function() 我可以創(chuàng)建某種例程來做到這一點(diǎn),但我不知道具體如何,因?yàn)檫@是我第一次使用 SQLITE 進(jìn)行開發(fā).
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.
如果有興趣,它是我正在開發(fā)的框架的 MVC 路由的一部分.
If interest, it's part of an MVC Routing of a Framework I'm developing.
非常感謝,提前.
推薦答案
您可以使用 SQLiteDatabase::createFunction
文檔 此處 或 PDO::sqliteCreateFunction
文檔 這里
You can use SQLiteDatabase::createFunction
documentation here or PDO::sqliteCreateFunction
documentation here
我做了這樣的事情:
<?php
function _sqliteRegexp($string, $pattern) {
if(preg_match('/^'.$pattern.'$/i', $string)) {
return true;
}
return false;
}
$PDO->sqliteCreateFunction('regexp', '_sqliteRegexp', 2);
?>
使用:
SELECT route FROM routes WHERE pattern REGEXP 'your/url/string' LIMIT 1
這篇關(guān)于要在 SQLITE SELECT 語句中使用的自定義 REGEXP 函數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!