ereg() problem for foreign language?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yavona
Forum Newbie
Posts: 1
Joined: Fri May 07, 2010 8:29 am

ereg() problem for foreign language?

Post by yavona »

Hello,

i'm trying to write a function for registration. But my problem is to accept turkish characters like öçişüğı.

First of all, i've tried;

if (ereg('[^a-z0-9]', $user))

but didn't work.

than i've added turkish chars like çöiüğşı adter [a-z] but didn't worked.

than i've worked on mb_ereg, mb_regex_encoding("UTF-8") but didn't worked again.

at the end i've this code;

mb_regex_encoding("UTF-8");
if(!mb_ereg("[a-z]",$_POST['user']))
{
echo "false";
} else {
echo "true";
}

when i write "öçiğüı" in input it accepted the turkish chars,
when i write forbidden chars "}][{{½' , it didn't accept as i wanted to be.
but when i write "test}][{{½" it accepted :)) and i'm still crying since that second.

At the end, i've a problem about accepting utf-8 chars with ereg .. Any idea or code sample do you have for this problem?

thanks to all
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: ereg() problem for foreign language?

Post by flying_circus »

Yes, regular expressions will make you rip all of your hair out.

the ereg extension is deprecated and you should use the PCRE functions instead. preg_match() can work with UTF8 encoded strings using the /u switch at the end.

check out:

http://us3.php.net/manual/en/ref.pcre.php

and

http://us3.php.net/manual/en/reference. ... ifiers.php

The reason your last example worked, and caused you to cry, is because the regex engine found an [a-z] char at position 0 in your supplied string (the "t" in "test").
Post Reply