[英]PHP Regex for font-size limit
I'm having some trouble with Regex never really used it. 我在Regex上遇到了麻烦,从未真正使用过它。 However basically I'm trying to set a limit on my font-size bbcode tag.
但是,基本上,我试图在我的字体大小bbcode标签上设置一个限制。
class SizeValidator implements \JBBCode\InputValidator
{
public function validate($input)
{
return (bool) preg_match('regex', $input);
}
}
If someone can help with the regex that'll be perfect! 如果有人可以帮助您使用正则表达式,那将是完美的! Basically just want Size 7 to 30 max, no px, em, nothing strictly numbers max 2 numbers if anyone with regex experience would be quite helpful possibly explain how it works so I can improve and get a better understanding :)
基本上只是想要7到30的最大值,没有px,em,没有任何严格的数字,最多2个数字,如果有正则表达式经验的人会非常有帮助,可能会解释它是如何工作的,所以我可以改进并更好地理解:)
There really is no reason to use regular expressions here. 确实没有理由在这里使用正则表达式。
Simply verify that what you're getting is a sequence of digits (for instance using ctype_digit
, and that the value lies between 7 and 30. 只需验证您得到的是一个数字序列(例如,使用
ctype_digit
,并且该值在7到30之间)。
class SizeValidator implements \JBBCode\InputValidator {
public function validate($input) {
return ctype_digit($input) && $input >= 7 && $input <= 30;
}
}
It's much more readable and easier to modify if need be. 如果需要,它更具可读性,更容易修改。
You could try something like this: 您可以尝试这样的事情:
return (bool)preg_match('/\[size=(([7-9])|([1-2]\d)|(30))\](.*)?\[\/size\]/', $input);
First I am matching if the number is 7-9, if so your function returns true. 首先,如果数字是7-9,我将进行匹配,如果是,则您的函数返回true。
([7-9])
Else if your number is with two digits starting with either 1 or 2 then the function also returns true 否则,如果您的数字是以1或2开头的两位数字,则该函数还会返回true
([1-2]\d)
Or else I check if the number is 30 and return true. 否则,我检查数字是否为30,然后返回true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.