SyntaxStudy
Sign Up
PHP PCRE Character Classes
PHP Intermediate 5 min read

PCRE Character Classes

PCRE Character Classes

PHP uses PCRE (Perl Compatible Regular Expressions). PCRE offers richer character classes than basic regex, including POSIX classes and Unicode properties.

Example
<?php
// Shorthand classes
"/\d/"   // Digit [0-9]
"/\D/"   // Non-digit
"/\w/"   // Word char [A-Za-z0-9_]
"/\s/"   // Whitespace
"/\S/"   // Non-whitespace

// POSIX classes (use inside character class)
"/[[:alpha:]]/"  // Letters
"/[[:digit:]]/"  // Digits
"/[[:alnum:]]/"  // Alphanumeric
"/[[:space:]]/"  // Whitespace
"/[[:punct:]]/"  // Punctuation

// Unicode categories (with u flag)
"/\p{L}/u"    // Unicode letters
"/\p{N}/u"    // Unicode numbers
"/\p{Z}/u"    // Unicode separators (spaces)
Pro Tip

Use the /u modifier for patterns on UTF-8 strings — without it, multibyte characters may be mishandled.