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
Release: 2024-12-18 17:26:09
Original
501 people have browsed it

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}'\- ])+$/
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template