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 an exact string?
- How to match a single quote in PHP regex?
- How to get last matched occurrence in PHP regex?
- How to match a double quote in PHP regex?
- How to use PHP regex to match a boolean value?
- How to use an "or" condition in PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match UUID?
- How to match a space using PHP regex?
- How to remove all non-numeric characters using PHP regex?
See more codes...