正则表达式php中的变量 [英] Variable inside regular expression php
本文介绍了正则表达式php中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我坚持使用正则表达式,我需要帮助.所以基本上我想做这样的事情:
I a stuck with regular expression and i need help. So basically i want to do somethning like this:
$data = "hi";
$number = 4;
$reg = '/^[a-z"]{1,4}$/';
if(preg_match($reg,$data)) {
echo 'Match';
}else {
echo 'No match';
}
但我想使用变量
$reg = '/^[a-z"]{1, variable here }$/';
我试过了:
$reg = '/^[a-z"]{1, '. $number .'}$/';
$reg = "/^[a-z\"]{1, $number}$/";
但没有得到正确的结果.
But not getting right result.
Tnx 寻求帮助
推荐答案
在第一个示例中,您有不应该有的空间,
In the first example you have space where you shouldn't have one,
你有:
$reg = '/^[a-z"]{1, '. $number .'}$/';
你应该有:
$reg = '/^[a-z"]{1,'. $number .'}$/';
然后它工作得很好
更新:您在第二个示例中遇到了同样的错误 - 感谢 AbraCadaver
Update: You have same error in second example - thanks to AbraCadaver
这篇关于正则表达式php中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文