3

I have a string like this :

این یafsک رشsegته می y34باشد

Specially , i want a function (like regex) to echo out JUST persian chars.

So , the out put should be :

این یک رشته می باشد

I have found we can use below regex, but i can't use it with function, it does not work.

regex i tried:

preg_match("(^[\x{0600}-\x{06FF}]*$)", $title);

error : Compilation failed: character value in \x{} or \o{} is too large at offset

What can i do and what should i do ?

Tnx.

Doon
  • 19,719
  • 3
  • 40
  • 44
Kala
  • 43
  • 1
  • 6

3 Answers3

19

سلام عزیزم

$string = preg_replace("/[a-zA-Z0-9]/", "", $string);

You can simply remove all English alphabetical characters and all numbers from the string

OR : you can do it reverse :

$string  = preg_replace("/[^ الف-ی]/i", "", $string);

This will remove all characters except Persian chars , This way you can remove all persian numbers too : d

stack
  • 10,280
  • 19
  • 65
  • 117
Milad
  • 27,506
  • 11
  • 76
  • 85
  • wow, tnx my dear friend, nice trick, sorry my reputation is low i can't vote up, + another problem is there any way to remove persian numbers too ? – Kala Nov 09 '14 at 19:18
  • Yeap I think my seccond recommendation will do the job , – Milad Nov 09 '14 at 19:21
4

You need to enable UTF-8 on the pattern to use the “higher” unicode range in a regex:

preg_match("/^[\x{0600}-\x{06FF}]*$/u", $title);

Note the u modifier after the closing delimiter.

lxg
  • 12,375
  • 12
  • 51
  • 73
  • Tnx, But it's not working, It returns `0` for every every string. – Kala Nov 09 '14 at 19:08
  • If you want to *collect* the matches, you have to pass a third parameter to preg_match which is filled by reference. See https://php.net/preg_match. However, my solution fixes the “compilation failed” error. Everything else depends on what you're actually trying to achieve. – lxg Nov 09 '14 at 19:10
  • 1
    If you want to remove them, see xe4me's answer. – lxg Nov 09 '14 at 19:11
1

Try this one:

preg_match("/^[الف-ی]*$/u", $title);