Home >Backend Development >PHP Tutorial >Why Doesn't My PHP Regex Match Unicode Characters?
Unicode Letter Character Matching in PCRE/PHP
In an attempt to validate names in PHP, a pattern is implemented utilizing the following regex:
/^([\p{L}'\- ])+$/
However, the validation fails to match Unicode characters such as Ă or 张.
Analysis
The issue stems from the omission of the "u" modifier in the regex. This modifier is necessary to enable Unicode character matching and avoid confusion with ASCII-only patterns.
By modifying the pattern to include the "u" modifier:
/^[-\' \p{L}]+$/u
the regex can now correctly match Unicode letter characters, as well as apostrophes, hyphens, and spaces.
The above is the detailed content of Why Doesn't My PHP Regex Match Unicode Characters?. For more information, please follow other related articles on the PHP Chinese website!