php-regexHow to use PHP regex to match a boolean value?
PHP regex can be used to match a boolean value by using the preg_match()
function. This function takes two parameters, the first being the regular expression pattern and the second being the string to match against.
$string = 'true';
$pattern = '/^(true|false)$/';
if (preg_match($pattern, $string)) {
echo 'Matched';
} else {
echo 'Not matched';
}
Output example
Matched
The code above uses the preg_match()
function to match a boolean value. The first parameter is the regular expression pattern /^(true|false)$/
which looks for either the string true
or false
at the beginning and end of the string. The second parameter is the string to match against, in this case true
. If the string matches the pattern, the preg_match()
function will return true
and the Matched
string will be printed. Otherwise, the Not matched
string will be printed.
Helpful links
More of Php Regex
- How to use PHP regex to match a nbsp HTML whitespace?
- How to match a space using PHP regex?
- How to use PHP regex to match an exact string?
- How to match a single quote in PHP regex?
- How to use PHP regex to match whitespace?
- How to match strings starting with a certain string using PHP regex?
- How to remove all non-numeric characters using PHP regex?
- How to remove non-printable characters using PHP regex?
- How to match a double quote in PHP regex?
- How to use PHP regex with the "x" modifier?
See more codes...