Knowledgebase

ereg, preg_replace with non-english characters?

Posted by lexington, 02-16-2010, 04:20 PM
Interesting, I never even knew about this problem. I created a script that works correctly with english based characters. A Russian guy is using the script on his site, and the russian characters are causing problems with predefined functions like eregi and preg_replace. So if I use the code: When using russian characters like Книги&Фильмы it causes the error to appear as it doesn't recognize them. Is there a built in feature to make it work with non-english text?

Posted by mattle, 02-16-2010, 06:58 PM
First of all, you shouldn't be using the ereg_* functions. They are deprecated and will not be included in PHP6. PCRE (implemented by the preg_* functions) provides '\w' which is basically "word characters" and is locale sensitive. I believe in English those include A-Za-z0-9\s, so you should be okay with '/[^\w_-]/'. More info on PCRE here: http://www.pcre.org/pcre.txt

Posted by lexington, 02-16-2010, 07:48 PM
Thanks, but i am too busy to take a college course on becoming an expert on using that. Could anyone please take the code I posted and modify it to work like suggested? Thanks.

Posted by mattle, 02-16-2010, 07:58 PM
I'm sure someone will...I, however, won't. Everything you need is in my first post and I gave you the de facto reference if you need more. If you don't understand something, I'll be happy to explain it to you, but I don't write code for people unless I'm getting paid.

Posted by route, 02-17-2010, 11:07 AM
interesting, thanks

Posted by tim2718281, 02-17-2010, 07:14 PM
There is a built-in function 'ctype_alnum' to check is a string is all letters and numerics. And you can use the set_locale function to specify the 'locale' - which allows you to specify alphabets. But the locales need to be installed on the running Linux system. You can see which locales are installed by executing as root: local -a When I run that on my system, I get 720 lines of output; the following are the lines for Russian locales: ru_RU ru_RU.iso88595 ru_RU.koi8r ru_RU.utf8 russian ru_UA ru_UA.koi8u ru_UA.utf8 If you install new locales, you'll need to restart Apache. OK, so here is a test PHP program; you should run it in your browser. I can't tell if it works, as I can't read Russian. When I run it, I get: The string ! does not consist of all letters or digits. The string foo!#$bar does not consist of all letters or digits. The string Книги&Фильмы does not consist of all letters or digits. The string К does not consist of all letters or digits. The string н consists of all letters or digits. The string и consists of all letters or digits. The string г consists of all letters or digits. The string и consists of all letters or digits. The string & does not consist of all letters or digits. The string Ф consists of all letters or digits. The string и consists of all letters or digits. The string л consists of all letters or digits. The string ь does not consist of all letters or digits. The string м consists of all letters or digits. The string ы does not consist of all letters or digits. Maybe someone who can read Russian can point out any errors in it. But what about the other characters (you had underscore, hyphen, and blank)? I suggest you write a function which deletes any of those from the input before checking the remainder. OK, that's a start. But note this warning from the PHP manual: Warning The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale(). Last edited by tim2718281; 02-17-2010 at 07:20 PM.

Posted by tim2718281, 02-17-2010, 07:41 PM
Dang; that should have been: locale -a not local -a



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
how secure DNS Server (Views: 571)
cron.daily help! (Views: 620)
webhosting.info down? (Views: 649)


Language:

Client Login

Email

Password

Remember Me

Search