我想出了一个办法做到这一点没有正则表达式,但是你的条件将仍然匹配:
function my_func($str) {
$letters = 'abcdefghijklmnopqrstuvwxyz';
$match = true; // Will be set to false if does not match conditions
$l1pos = strrpos($letters, $str[0]);
$l2pos = strrpos($letters, $str[1]);
$l3pos = strrpos($letters, $str[2]);
$l4pos = strrpos($letters, $str[3]);
$l5pos = strrpos($letters, $str[4]);
// If letter 2 comes before letter 1
if ($l2pos < $l1pos) { $match = false;}
// If letter 3 comes before letter 2
if ($l3pos < $l2pos) { $match = false; }
// If letter 4 comes after letter 3
if ($l4pos >= $l3pos) { $match = false; }
// If letter 5 comes after letter 4
if ($l5pos >= $l4pos) { $match = false; }
return $match;
}
您可以使用它像这样:
$string = 'apple';
if (my_func($string)) {
print 'Matched!';
}
else {
print 'Not Matched. :(';
}
如果您想要使功能真的很小,您可以使用以下内容:
function my_func($str) {
$letters = 'abcdefghijklmnopqrstuvwxyz';
$match = true;
function m($i) {
return strrpos($letters, $str[$1]);
}
if ((m(1) < m(0)) || (m(2) < m(1)) || (m(3) >= m(2)) || (m(4) >= m(3))) {
$match = false;
}
return $match;
}
我也实验正则表达式,并得到了以下内容:
^([a-z]) # First Letter
([\1-z]) # Second Letter
([\2-z]) # Third Letter
([a-\3]) # Fourth Letter
([a-\4]) # Fifth Letter
但是,您不能使用a-z
而动态地设置a
或z
到先前拍摄的群体之一。您可以使用PHP连接来创建RegEx,但是这需要至少4行代码,除了第一个字母外。
我创建了以下内容,它只能工作到第三个字母。第四和第五有错误。希望你能看到我想要做的逻辑...... [DEMO](https://regex101.com/r/nH2fJ7/1) – Druzion
你可以使用['ord()'](http:// php.net/manual/en/function.ord.php)[[demo]](https://eval.in/543423)来比较[ASCII值](http://www.ascii-code.com/ )。在MySQL中[自定义函数](http://dev.mysql.com/doc/refman/5.7/en/adding-functions.html)可以被添加,但它可能是一些努力。用MySQL正则表达式我认为这是不可行的。 –