Tell me how to use the strpos ( stripos ) function to find the position of the occurrence of any Cyrillic letter? If there is a specific phrase, then there is no problem. BUT, here's what to do if I don’t know what the FIRST letter from the Russian alphabet can be in a line (almost the entire line is in english)?

  • for multibyte encodings there seems to be mb_strpos() . - Edward

1 answer 1

Honestly, I didn’t quite understand the essence of the question, but if the question is to find the first character from Cyrillic, then you can:

 $string = 'qwerty Привет qerty'; preg_match('/[а-я]/iu', $string, $matches, PREG_OFFSET_CAPTURE); var_dump($matches[0]); 

The result will be:

 array(2) { [0] => string(2) "П" [1] => int(7) } 

In the array, the first character and its position.


Through strpos , or rather mb_stripos :

 function mb_stripos_array($haystack, $needles = array(), $offset = 0) { $chr = array(); foreach ($needles as $needle) { $res = mb_stripos($haystack, $needle, $offset); if ($res !== false) $chr[$needle] = $res; } if (empty($chr)) return false; return min($chr); } $range = []; for ($i = 224; $i <= 255; $i++) { $range[] = iconv('CP1251', 'UTF-8', chr($i)); } $string = 'qwerty Привет qerty'; var_dump(mb_stripos_array($string, $range)); 

mb_stripos_array result is similar to the mb_stripos result. Those. returns the first occurrence, but if you still need to know which character, then:

 return min($chr); 

Replace with:

 $value = min($chr); return [mb_substr($haystack, $value, 1), $value]; 
  • Manitikyl, but do not tell me, maybe there is a solution to this issue by means of strpos, and not by a regular schedule? - Nikolay Ageev
  • @ Nikolay Ageyev I added my version to my answer through strpos , but this is a wild crutch, I don’t even know why you need this ... - Manitikyl