How to Check if String Is a Hexadecimal Value in PHP?

Check if string is a valid hexadecimal value

In PHP, you can check if a string is a hexadecimal value or not, in the following ways:

Using ctype_xdigit()

The built-in ctype_xdigit() function in PHP checks if the provided string is a valid hexadecimal value, and returns true or false accordingly. However, it does not take into account strings starting with a hexadecimal radix prefix (i.e. 0x or 0X). To consider those strings too, you could do the following:

  1. If string starts with 0x (or 0X), then remove those and continue;
  2. Check if remaining string is a valid hexadecimal value using ctype_xdigit().

For example:

function isHex(string $str): bool {
    if (str_starts_with(strtolower($str), '0x')) {
        $str = substr($str, 2);
    }

    return ctype_xdigit($str);
}

var_dump(isHex('123')); // true
var_dump(isHex('aabbcc')); // true
var_dump(isHex('FBfbFb')); // true
var_dump(isHex('0x123abc')); // true
var_dump(isHex('0X123abc')); // true

var_dump(isHex('0x')); // false
var_dump(isHex('0xx123')); // false
var_dump(isHex('123foo')); // false
var_dump(isHex('foo')); // false
var_dump(isHex('0xfoo')); // false
var_dump(isHex('0Xfoo')); // false
var_dump(isHex('1230xabc')); // false

Using a Regular Expression

You could check a string against a regular expression pattern to see if all characters are within the range of hexadecimal character set, and accordingly return true or false. This could be implemented like so:

  1. Allow strings that start with 0x (or 0X);
  2. Check if string is at least one character long and only contains characters within hexadecimal character range, and accordingly return true or false.

For example:

function isHex(string $str): bool {
    return preg_match('/^(?:0x)?[a-f0-9]{1,}$/i', $str);
}

var_dump(isHex('123')); // true
var_dump(isHex('aabbcc')); // true
var_dump(isHex('FBfbFb')); // true
var_dump(isHex('0x123abc')); // true
var_dump(isHex('0X123abc')); // true

var_dump(isHex('0x')); // false
var_dump(isHex('0xx123')); // false
var_dump(isHex('123foo')); // false
var_dump(isHex('foo')); // false
var_dump(isHex('0xfoo')); // false
var_dump(isHex('0Xfoo')); // false
var_dump(isHex('1230xabc')); // false

Trimming Hexadecimal Characters and Checking for Empty String

You could trim all hexadecimal characters from a string, leaving an empty string, and accordingly, you could return true or false. This could be implemented like so:

  1. Explicitly check if string only contains the radix prefix, and return early if it is indeed the case;
  2. Use substr() to strip out hexadecimal radix prefix (i.e. 0x and 0X), if any;
  3. Use trim() function to strip out all hexadecimal characters by specifying a range of characters (using "..");
  4. Check if the remainder is an empty string, and accordingly return true or false.

For example:

function isHex(string $str): bool {
    $lowecaseStr = strtolower($str);

    if ($lowecaseStr === '0x') {
        return false;
    }

    if (str_starts_with($lowecaseStr, '0x')) {
        $str = substr($str, 2);
    }

    return trim($str, '0..9A..Fa..f') === '';
}

var_dump(isHex('123')); // true
var_dump(isHex('aabbcc')); // true
var_dump(isHex('FBfbFb')); // true
var_dump(isHex('0x123abc')); // true
var_dump(isHex('0X123abc')); // true

var_dump(isHex('0x')); // false
var_dump(isHex('0xx123')); // false
var_dump(isHex('123foo')); // false
var_dump(isHex('foo')); // false
var_dump(isHex('0xfoo')); // false
var_dump(isHex('0Xfoo')); // false
var_dump(isHex('1230xabc')); // false

Hope you found this post useful. It was published . Please show your love and support by sharing this post.