Tag Archives: regex

Regex 01 – Alphanumeric Strings

// Allow alphanumeric only
$pattern = "/^[A-Za-z0-9]+$/";

// Allow alphanumeric with spaces
$pattern = "/^[A-Za-z0-9 ]+$/ ";

// Allow alphanumeric with underscore and dashes
$pattern = "/^[A-Za-z0-9_-]+$/";

// Allow alphanumeric with underscore, dashes, and spaces
$pattern = "/^[A-Za-z0-9_- ]+$/";

Testing Strings Using preg_match()

if( preg_match( $pattern, "This is a string 1234" ) ) {
    echo "Match";
} else {
    echo "No match";
}