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 to match a multiline?
- How to match a quotation mark in PHP regex?
- How to get last matched occurrence in PHP regex?
- How to match a single quote in PHP regex?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match special characters?
- How to use PHP regex to match a zip code?
- How to use PHP regex to match a year?
- How to match a space using PHP regex?
See more codes...