Hello!
In general, simple code:

$domain = 'сайт.рф'; if(@preg_match("/^[0-9a-zа-я-.]+\.[0-9a-zа-я-]{2,6}$/u", $domain)) { $enter = true; } else { $enter = false; } // возвращает true 

  $domain = 'сайт.рф'; if(@preg_match("/^[0-9a-zа-я-.]+\.[0-9a-zа-я-]{2,6}$/u", $domain) && @preg_match("/^[0-9a-zа-я]$/u", $domain[0])) { $enter = true; } else { $enter = false; } // возвращает false 

I don’t understand why in the second version I enter false.

    2 answers 2

    Because Unicode is two (four) byte encoding (in your case, two-), and the call is

     $unicode[$x] 

    will return one byte, which, of course, is not a letter. Use functions for working with Unicode or rewrite a regular expression to one that replaces two conditions with one expression:

     /^[0-9a-zа-я][0-9a-zа-я-.]*\.[0-9a-zа-я-]{2,6}$/u 

    or:

     /^(?![-.])[0-9a-zа-я-.]+\.[0-9a-zа-я-]{2,6}$/u 

      In the first condition, you are checking the $ domain variable

       preg_match("/^[0-9a-zа-я-.]+\.[0-9a-zа-я-]{2,6}$/u" 

      which contains letters, numbers, hyphens, and points to a point, and 2 to 6 numbers, letters, or hyphens after a point. The condition is specified incorrectly, the hyphen and the point must be separated by a backslash, i.e.

        preg_match("/^[0-9a-zа-я\-\.]+\.[0-9a-zа-я\-]{2,6}$/u" 

      In the second case, you are trying to check the variable as an array, which leads to an error and the condition is met otherwise (else)

       && @preg_match("/^[0-9a-zа-я]$/u", $domain[0]) 

      $ domain [0] is not an array. Remove all @ in the condition before preg_match, do not turn off the error output, and this prevents you from debugging.

      • @Ravel, in the character class, you can not escape the point and hyphen. True hyphen should either be at the beginning, or at the end of the character class then. It is possible to refer to strings as an array, but only if the string is Unicode, then this will do little if it is not necessary to work with Unicode bytes. - ReinRaus
      • @ReinRaus, are you sure that he has a string in Unicode? As for screening, everyone decides for himself here, but special characters recommend screening, and it is easier to read. He simply closed all the errors he got out of @, he didn’t see the problem. - Ravel
      • The condition is not set correctly hyphen and the point must be separated by a slash ie. preg_match ("/ ^ [0-9a-zа-я \ - \.] + \. [0-9a-zа-я \ -] {2,6} $ / u" And why do I need to separate the backslash? What does it give? - gtx1024