diff --git a/src/Utils.php b/src/Utils.php index 1d1fd50..f581ca1 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -118,7 +118,7 @@ class Utils if (!is_string($value)) { throw new InvalidArgumentException('The value to zeroPrefixed function must be string.'); } - return (strpos($value, '0x') === 0); + return (strpos($value, '0x') === 0) ; } /** @@ -178,6 +178,17 @@ class Utils return true; } + /** + * isHex + * + * @param string + * @return bool + */ + public static function isHex($value) + { + return (is_string($value) && preg_match('/^(0x)?[a-f0-9]*$/', $value) === 1); + } + /** * sha3 * keccak256 diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index 6c683ea..84b4522 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -366,4 +366,24 @@ class UtilsTest extends TestCase $this->assertEquals($jsonArrayDepth2, $jsonAssoc); } + + /** + * testIsHex + * + * @return void + */ + public function testIsHex() + { + $isHex = Utils::isHex($this->testHex); + + $this->assertTrue($isHex); + + $isHex = Utils::isHex('0x' . $this->testHex); + + $this->assertTrue($isHex); + + $isHex = Utils::isHex('hello world'); + + $this->assertFalse($isHex); + } } \ No newline at end of file