I want filter input just for Farsi, i think regular expression with filter_input()
is a good idea but
i dont know how solve this problem, I've search but i don't find sth like \p{Arabic} for Farsi or Persian character in the other hand it can be possible with Unicode but i've no idea about its regular expression.
Asked
Active
Viewed 3,029 times
6

user1695063
- 99
- 1
- 8
-
Have you tried `\p{Arabic}` and see if it works? `Arabic` refers to the script (which includes different Unicode blocks) for languages that uses Arabic scripts. – nhahtdh Mar 02 '13 at 19:31
-
No because Persia has 4 more character than Arabic has. – user1695063 Mar 03 '13 at 05:28
-
I mean the Unicode Arabic script, not the Arabic language or Persian language itself. The language may have different, but does the Unicode Arabic script support everything? (Try to type it out here: http://www.regex101.com/r/tZ9dB4). If it does not match, can you show the example? – nhahtdh Mar 03 '13 at 06:48
-
thanks this site is very awesome i tested and its work for persian but when i test it in PHP it doesn't work at all, – user1695063 Mar 03 '13 at 20:05
-
$text = 'ads'; $regex = '/^\p{Arabic}+$/'; $itFoundMatch = preg_match( $regex, $text ); //$itFoundMatch == false – user1695063 Mar 03 '13 at 20:06
-
`ads` is not even Persian script! – nhahtdh Mar 03 '13 at 22:05
-
yes it is not this is a example because when i write sth like "سیب" (means apple) php convert it to سیب i still don't know how use it. i want get text from the user and check it whether is Persia or English. – user1695063 Mar 04 '13 at 12:11
-
How do you take in user input? (Edit your question, please). – nhahtdh Mar 04 '13 at 12:22
-
this question is solved by you, thanks. but i have another problem with php assign. I'm going to take the text with form's field and post it to process page so that validation it with filter_input() method but php ignore Arabic font in its variable. – user1695063 Mar 04 '13 at 13:52
-
Without any code, I don't know where to start answering your question. – nhahtdh Mar 04 '13 at 13:54
-
this problem solved but when i want echo "فارسی"; php shows sth like سیب – user1695063 Mar 04 '13 at 14:33
1 Answers
10
This should work :
preg_match("(^[\x{0600}-\x{06FF}]*$)", 'فارسی');
And also you can use all these forms for Persian language :
Persian Alphabet
فارسی
/^([\x{0600}-\x{06EF}])+$/
Persian Numbers
۱۲۳۴۵۶۷۸۹۰
/^([\x{06F0}-\x{06F9}])+$/
Persian simple text with space and half-space
اگر زمان و مکان در اختیار ما بود، ۱۰ سال پیش از طوفان نوح عاشقت میشدم
/^([\x{0600}-\x{06FF}| |\x{200C}])+$/

Mehdi Hosseini
- 1,937
- 1
- 18
- 29
-
this problem solved but how can i show the result something like echo "فارسی"; – user1695063 Mar 04 '13 at 14:30
-
preg_match just checks if expression is in 0600 and 06FF then tell you it's true or not, you mean you can't print 'فارسی' in html page? – Mehdi Hosseini Mar 04 '13 at 14:35
-
preg_match returns 1 that indicates success but when i want print Arabic string php prints weird character sth like سیب – user1695063 Mar 04 '13 at 14:51
-
the problem is in your document Encoding, your document should be saved in UTF-8 without boom, you can use notepad++ or sublime text and in file menu change document encoding, and then you should define UTF-8 in your html page meta data, Just paste this in your html header : `` – Mehdi Hosseini Mar 04 '13 at 14:57
-
yeah :) you're right that's it, thanks about your helping so my problem is about Encoding. thanks all of you guys. – user1695063 Mar 04 '13 at 16:06
-
1What is the last `\x` in `[\x{0600}-\x{06FF}\x]`? I don't understand what it really does. – Mohammad Naji Feb 24 '14 at 15:56
-
1Oops! there was a mistake on my regular expression, you right @MohammadNaji i updated expression. – Mehdi Hosseini Mar 12 '14 at 11:53