SyntaxStudy
Sign Up
PHP Unicode Regex in PHP
PHP Advanced 5 min read

Unicode Regex in PHP

Unicode and PCRE

Add the u modifier to handle multibyte Unicode characters correctly. Use \p{...} for Unicode property classes.

Example
<?php
// Match Unicode letters (any script)
preg_match("/^\p{L}+$/u", "Привет"); // matches Russian
preg_match("/^\p{L}+$/u", "مرحبا");  // matches Arabic
preg_match("/^\p{L}+$/u", "Hello");  // matches English

// Match Unicode digits (includes Arabic-Indic numerals)
preg_match("/\p{Nd}+/u", "١٢٣");  // Arabic-Indic digits

// Count characters correctly (mb_ functions)
echo mb_strlen("café");      // 4 (not strlen: 5 bytes)

// Unicode word boundary issues
preg_match("/\bjava\b/iu", "JavaScript"); // may not work as expected with Unicode
Pro Tip

The /u modifier makes . match Unicode characters correctly — without it, multibyte characters are treated as multiple bytes.