Crypto Transaction Hash (Generic) Regex for PHP
/^(?:0x)?[0-9a-fA-F]{64}$/What this pattern does
This page provides a lightweight, single-purpose regular expression for matching crypto transaction hash (generic), ported and verified for PHP. Financial data validation has zero tolerance for false negatives — a missed invalid entry can corrupt downstream calculations. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// Crypto Transaction Hash (Generic)
// ReDoS-safe | RegexVault — Finance > Crypto
define('CRYPTO_TRANSACTION_HASH_GENERIC_PATTERN', '/^(?:0x)?[0-9a-fA-F]{64}$/');
function validate_crypto_transaction_hash_generic(string $input): bool {
return (bool) preg_match(CRYPTO_TRANSACTION_HASH_GENERIC_PATTERN, $input);
}
// Example
var_dump(validate_crypto_transaction_hash_generic("0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789 | 0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1b |
abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789 | 0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789a |
a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2 | notahash |
| — | 0xGGGG |
When to use this pattern
This pattern is drawn from the Finance > Crypto category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Transaction hashes are not globally unique across blockchains — the same hash string could theoretically appear on different chains (extremely unlikely but true). Include the chain identifier in your data model.
Technical Notes
Ethereum transactions use the 0x prefix. Bitcoin transactions do not. Both produce 256-bit (64 hex chars) hashes. Solana uses Base58-encoded 64-byte hashes — use a different pattern for Solana txids.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern