Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I match accented characters in preg_match()?

I've read how accented characters might sometimes match [a-z]. What I'd like to know is how I could match a specific accented character. Obviously, preg_match('/[ñ]/', 'ñ') does not work.

like image 595
Nikki Erwin Ramirez Avatar asked Feb 12 '10 08:02

Nikki Erwin Ramirez


People also ask

What is Preg_match () function?

PHP preg_match() function. The preg_match() function is a built-in function of PHP that performs a regular expression match. This function searches the string for pattern, and returns true if the pattern exists otherwise returns false. Generally, the searching starts from the beginning of $subject string parameter.

What is the return value of Preg_match () function?

Return Values ¶ preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false . Please read the section on Booleans for more information.

How do I match a string in PHP?

The strcmp() function compares two strings. Note: The strcmp() function is binary-safe and case-sensitive. Tip: This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncmp().

What is the difference between Preg_match and Preg_match_all?

preg_match stops looking after the first match. preg_match_all , on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.


2 Answers

Use the /u modifier. That will enable Unicode for the regexes. http://php.net/manual/en/reference.pcre.pattern.modifiers.php

like image 102
Jimmie Lin Avatar answered Nov 14 '22 19:11

Jimmie Lin


You can take their codes and match them like \xD0 - heximal sequences if accented symbols are not accepted

like image 35
Vladislav Rastrusny Avatar answered Nov 14 '22 19:11

Vladislav Rastrusny