Posts tagged with “regular expression

Validating the format of a universally unique identifier

Here’s a function to validate the format of a universally unique identifier, or UUID: function uuid_is_valid( $uuid ) { // check string length if( strlen( $uuid ) != 36 ) return false; // no good // check formatting return preg_match( ‘/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89AB][0-9a-f]{3}-[0-9a-f]{12}$/i’, $uuid ) ? true : false; } It’s expecting a 36-character UUID. Adjust this […]

Strip all non-alphanumeric characters from a string

Here’s a very handy little snippet for stripping all non-alphanumeric characters from a string: $string = preg_replace(“/[^A-Za-z0-9]/”, “”, $string); This will leave all letters (whether they are capitalized or lowercase) as well as numbers 0 through 9.