Home >Backend Development >PHP Tutorial >Why Doesn't My PHP Regex Match Unicode Characters?

Why Doesn't My PHP Regex Match Unicode Characters?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-18 17:26:09499browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn