简体   繁体   中英

PHP Regex for font-size limit

I'm having some trouble with Regex never really used it. However basically I'm trying to set a limit on my font-size bbcode tag.

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 :)

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.

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])

Else if your number is with two digits starting with either 1 or 2 then the function also returns true

([1-2]\d)

Or else I check if the number is 30 and return true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM