php-regexHow to use PHP regex to match UTF8?
PHP regex can be used to match UTF8 strings by using the u
modifier. This modifier enables the pattern to be treated as a UTF-8 string.
Example code
$string = 'This is a UTF-8 string';
$pattern = '/^This/u';
if (preg_match($pattern, $string)) {
echo 'Match found';
}
Output example
Match found
Code explanation
$string
: This is the string that will be tested against the pattern.$pattern
: This is the pattern that will be used to match the string. Theu
modifier is used to enable UTF-8 matching.preg_match()
: This is the function used to match the string against the pattern.
Helpful links
More of Php Regex
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match an exact string?
- How to match a space using PHP regex?
- How to match a single quote in PHP regex?
- How to match a double quote in PHP regex?
- How to use an "or" condition in PHP regex?
- How to use regex in PHP to match dot character?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match an XML tag?
See more codes...