fork download
  1. <?php
  2.  
  3. // Our string
  4. $test_string = "Хуле ты пиздишь, что не нельзя, когда можно!";
  5.  
  6. /**
  7.  * Unicode-friendly version of str_split()
  8.  *
  9.  * @param string $string String to convert
  10.  * @param int $split_length Length of the maximum split fragment
  11.  */
  12. function str_split_unicode( $string, $split_length = 0 ) {
  13. if ( $split_length > 0 ) {
  14. $result = array();
  15. $string_length = mb_strlen( $string, "UTF-8" );
  16. for ( $index = 0; $index < $string_length; $index += $split_length ) {
  17. $result[] = mb_substr( $string, $index, $split_length, "UTF-8" );
  18. }
  19. return $result;
  20. }
  21.  
  22. return preg_split( "//u", $string, -1, PREG_SPLIT_NO_EMPTY );
  23. }
  24.  
  25. // Make the magic happen
  26. foreach ( str_split_unicode( $test_string ) as $index => $part ) {
  27. echo "Character: {$part}, Index: {$index}\n";
  28. }
Success #stdin #stdout 0.01s 82944KB
stdin
Standard input is empty
stdout
Character: Х, Index: 0
Character: у, Index: 1
Character: л, Index: 2
Character: е, Index: 3
Character:  , Index: 4
Character: т, Index: 5
Character: ы, Index: 6
Character:  , Index: 7
Character: п, Index: 8
Character: и, Index: 9
Character: з, Index: 10
Character: д, Index: 11
Character: и, Index: 12
Character: ш, Index: 13
Character: ь, Index: 14
Character: ,, Index: 15
Character:  , Index: 16
Character: ч, Index: 17
Character: т, Index: 18
Character: о, Index: 19
Character:  , Index: 20
Character: н, Index: 21
Character: е, Index: 22
Character:  , Index: 23
Character: н, Index: 24
Character: е, Index: 25
Character: л, Index: 26
Character: ь, Index: 27
Character: з, Index: 28
Character: я, Index: 29
Character: ,, Index: 30
Character:  , Index: 31
Character: к, Index: 32
Character: о, Index: 33
Character: г, Index: 34
Character: д, Index: 35
Character: а, Index: 36
Character:  , Index: 37
Character: м, Index: 38
Character: о, Index: 39
Character: ж, Index: 40
Character: н, Index: 41
Character: о, Index: 42
Character: !, Index: 43