user198989
user198989

Reputation: 4665

Allow only letters, numbers and some foreign letters?

I only want to allow users to use letters, numbers and some foreign letters. This works but it removes foreign letters (like öçşğ).

preg_replace('/[^a-zA-Z0-9\s]/', '', $nick)

So I have used below to allow some foreign letters, but it still removes the foreign letters.

preg_replace("@[^A-Za-z0-9\- şıüğçİŞĞÜÇ]+@i","",$nick);

What is the correct approach for allow them ?

Upvotes: 2

Views: 156

Answers (2)

tanerkay
tanerkay

Reputation: 3930

Close. Just need the u (PCRE_UTF8) modifier and you missed the letter "ö" ;)

preg_replace("@[^A-Za-z0-9\-\sşıüöğçİŞĞÜÖÇ]+@iu", '', 'Öm$ür Y_ağız');

returns Ömür Yağız

Untested, but you may be able to simplify the regex to:

@[^a-z0-9şıüöğçİ -]@iu

Upvotes: 1

Dmitriy.Net
Dmitriy.Net

Reputation: 1500

When you want to use language specific characters, you can use following construction, which more understandable than write all specific characters of your language in regular expression:

preg_replace("/[^\w\p{Latin}]+/", '', 'Öm$ür Y_ağız');

And replace "Latin" to your own unicode character set from http://php.net/manual/en/regexp.reference.unicode.php

Upvotes: 2

Related Questions