1

Here is my code:

function isValid($string) {
    return strlen($string) >= 6 &&
           strlen($string) <= 40 &&
           preg_match("/\d/", $string) &&
           preg_match("/[a-zA-Z]/", $string);
}

// Negative test cases
assert(!isValid("hello"));

// Positive test cases
assert(isValid("abcde2"));

As you see, my script validates a string based on 4 conditions. Now I'm trying to develop this one:

preg_match("/[a-zA-Z]/", $string)

This condition returns true just for English letters. How can I also add other letters like ا ب ث چ. Well how can I do that?

Note: Those characters aren't Arabic, they are Persian.

stack
  • 10,280
  • 19
  • 65
  • 117
  • 1
    Use `preg_match('/\p{L}/u', $string)` and replace `strlen` with `mb_strlen`. – Wiktor Stribiżew Jul 22 '16 at 08:57
  • @WiktorStribiżew Ah ok, just may you please tell me what's the meaning of `\p` and `{l}`? – stack Jul 22 '16 at 08:58
  • 1
    `\p{L}` matches any Unicode character. It does not match diacritics though. Perhaps, you need `preg_match('/\p{L}\p{M}*+/u', $string)`. Please check if that works for you. – Wiktor Stribiżew Jul 22 '16 at 08:59
  • @WiktorStribiżew Well actually I don't need to match diacritics characters. All I'm trying to do is: checking whether is the string containing a letter or not? *(that letter can be a English letter or Persian letter)* – stack Jul 22 '16 at 09:02
  • 1
    Ah, ok, Persian and English? `preg_match('/[\x{0600}-\x{06FF}A-Z]/iu', $string)` – Wiktor Stribiżew Jul 22 '16 at 09:04
  • @WiktorStribiżew Either Persian or English or even both of them. Anyway I need to see a letter to validate that string. Btw why you don't write an answer? – stack Jul 22 '16 at 09:05
  • I need to know what you need before answering :) – Wiktor Stribiżew Jul 22 '16 at 09:07

1 Answers1

1

To match either an English or Persian letter, you may use

preg_match('/[\x{0600}-\x{06FF}A-Z]/iu', $string)

The \x{0600}-\x{06FF} range is supposed to match all Persian letters. The A-Z range will match all ASCII letters (both upper- ans lowercase since the /i case insensitive modifier is used). The /u modifier is necessary since you are working with Unicode characters.

Also, use mb_strlen rather than strlen when checking a Unicode string length, it will count the Unicode code points correctly.

As for

Your password should be containing at least a letter (that letter can be in any language

You need to use

preg_match('/\p{L}/u', $string)

or

preg_match('/\p{L}\p{M}*+/u', $string)
             ^^^^^^^^^^^^

that will match any letter (even the one with a diacritic after it). \p{L} matches any base Unicode letter, and \p{M}*+ will possessively match 0+ diacritics after it. If the match value is not used, /\p{L}/u will suffice for the check.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Great .. upvote. Just as a note, that string is a password, and I have this rule to validate the password: *"your password should be containing at least a letter (that letter can be in any language, like EN, or FA or ..)"*. So do you think your pattern still is fine? – stack Jul 22 '16 at 09:09
  • You are redefining requirements on the fly, you know? It is not a good thing. – Wiktor Stribiżew Jul 22 '16 at 09:10