masih arastooyi
masih arastooyi

Reputation: 545

preg_match rule for utf-8

what pregmach rule i must use for full_name field?
i want user input only character and not html or php code value and space between 3 to 11 character
i can use:

<?php
if (preg_match("%^[A-Za-z0-9-_]{3,10}$%", $_REQUEST['usr'])) {
//do something like 
mysql_query('insert into user(name) values(this field)');
//damn what is this for: It does not meet our quality standards.!!!
//i must insert more code? i dont have ! let me go !
}
else{
//do something else!
die('get out !:D');
}
?>

but with this user cant input UTF-8 characters like "مسیح ارسطوئی"
so what preg_match rule i must use for UTF-8 ?
or what other code like preg_match i can use ?
I WANT USER JUST Can insert characters not <>{}[] or $%^&*
between 3 to 10 character !!! thanks

Upvotes: 2

Views: 8152

Answers (3)

Mehrdad Shoja
Mehrdad Shoja

Reputation: 67

preg_match_all('/#(\pL*)/u', 'this is #مثال text', $matches);
print_r($matches);

'u', '\pL': strings are treated as UTF-8.

Upvotes: 1

Kerem
Kerem

Reputation: 11586

This will give "0", cos مسیح ارسطوئی is not containing only 3-10 chars;

$x = preg_match('~^([\pL]{3,10})$~u', 'مسیح ارسطوئی');
echo $x ? 1 : 0;

But this gives a result in your case;

preg_match('~([\pL]+)~u', 'مسیح ارسطوئی', $m);
print_r($m);

Array
(
    [0] => مسیح
    [1] => مسیح
)

See more details here: PHP: Unicode character properties

Upvotes: 2

clover
clover

Reputation: 5170

Use u modifier like this:

preg_match('/pattern_with_unicode_symbols/u');

This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8.

and use "\x{2460}" syntax to define utf-8 character

Upvotes: 2

Related Questions