php-regexHow to use PHP regex to match a hexadecimal?
Using PHP regex to match a hexadecimal is quite simple. The following example code block will match a hexadecimal string of any length:
$hexadecimal = preg_match('/^[0-9A-Fa-f]+$/', $string);
The output of the above code will be 1
if the string is a valid hexadecimal, and 0
if it is not.
Code explanation
/^
: This is the start of the regular expression.[0-9A-Fa-f]
: This is the character class that matches any hexadecimal character.+
: This is a quantifier that matches one or more of the preceding character class.$/
: This is the end of the regular expression.
Helpful links
More of Php Regex
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match special characters?
- How to use PHP regex to match a year?
- How to use PHP regex to match an exact string?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match an XML tag?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match UUID?
- How to use PHP regex to match a zip code?
See more codes...