3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * By Darien Hager, Jan 2007... Use however you wish, but please * please give credit in source comments. * * Change "UTF-8" to whichever encoding you are expecting to use. */ function ords_to_unistr($ords, $encoding = 'UTF-8') { // Turns an array of ordinal values into a string of unicode characters $str = ''; for ($i = 0; $i < count($ords); $i++) { // Pack this number into a 4-byte string // (Or multiple one-byte strings, depending on context.) $v = $ords[$i]; $str .= pack("N",$v); } $str = mb_convert_encoding($str,$encoding,"UCS-4BE"); return($str); } function unistr_to_ords($str, $encoding = 'UTF-8') { // Turns a string of unicode characters into an array of ordinal values, // Even if some of those characters are multibyte. $str = mb_convert_encoding($str,"UCS-4BE",$encoding); $ords = array(); // Visit each unicode character for ($i = 0; $i < mb_strlen($str,"UCS-4BE"); $i++) { // Now we have 4 bytes. Find their total // numeric value. $s2 = mb_substr($str,$i,1,"UCS-4BE"); $val = unpack("N",$s2); $ords[] = $val[1]; } return($ords); } function alphabet($firstCharacter = 'A', $lastCharacter = 'Z') { $firstCharacterValue = unistr_to_ords($firstCharacter)[0]; $lastCharacterValue = unistr_to_ords($lastCharacter)[0]; if ($firstCharacterValue < $lastCharacterValue) { for ($character = $firstCharacterValue; $character <= $lastCharacterValue; ++$character) { yield ords_to_unistr(array($character)); }; } else { for ($character = $firstCharacterValue; $character >= $lastCharacterValue; --$character) { yield ords_to_unistr(array($character)); }; } } foreach (alphabet('ا', 'ی') as $letter) { echo $letter, PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/CTWqo
function name:  (null)
number of ops:  11
compiled vars:  !0 = $letter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   INIT_FCALL                                               'alphabet'
          1        SEND_VAL                                                 '%D8%A7'
          2        SEND_VAL                                                 '%DB%8C'
          3        DO_FCALL                                      0  $1      
          4      > FE_RESET_R                                       $2      $1, ->9
          5    > > FE_FETCH_R                                               $2, !0, ->9
   55     6    >   ECHO                                                     !0
          7        ECHO                                                     '%0A'
   54     8      > JMP                                                      ->5
          9    >   FE_FREE                                                  $2
   56    10      > RETURN                                                   1

Function ords_to_unistr:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 5
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 5
Branch analysis from position: 16
Branch analysis from position: 5
filename:       /in/CTWqo
function name:  ords_to_unistr
number of ops:  24
compiled vars:  !0 = $ords, !1 = $encoding, !2 = $str, !3 = $i, !4 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'UTF-8'
   11     2        ASSIGN                                                   !2, ''
   12     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->13
   15     5    >   FETCH_DIM_R                                      ~7      !0, !3
          6        ASSIGN                                                   !4, ~7
   16     7        INIT_FCALL                                               'pack'
          8        SEND_VAL                                                 'N'
          9        SEND_VAR                                                 !4
         10        DO_ICALL                                         $9      
         11        ASSIGN_OP                                     8          !2, $9
   12    12        PRE_INC                                                  !3
         13    >   COUNT                                            ~12     !0
         14        IS_SMALLER                                               !3, ~12
         15      > JMPNZ                                                    ~13, ->5
   18    16    >   INIT_FCALL                                               'mb_convert_encoding'
         17        SEND_VAR                                                 !2
         18        SEND_VAR                                                 !1
         19        SEND_VAL                                                 'UCS-4BE'
         20        DO_ICALL                                         $14     
         21        ASSIGN                                                   !2, $14
   19    22      > RETURN                                                   !2
   20    23*     > RETURN                                                   null

End of function ords_to_unistr

Function unistr_to_ords:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 11
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 11
Branch analysis from position: 33
Branch analysis from position: 11
filename:       /in/CTWqo
function name:  unistr_to_ords
number of ops:  35
compiled vars:  !0 = $str, !1 = $encoding, !2 = $ords, !3 = $i, !4 = $s2, !5 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'UTF-8'
   25     2        INIT_FCALL                                               'mb_convert_encoding'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'UCS-4BE'
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $6      
          7        ASSIGN                                                   !0, $6
   26     8        ASSIGN                                                   !2, <array>
   29     9        ASSIGN                                                   !3, 0
         10      > JMP                                                      ->27
   32    11    >   INIT_FCALL                                               'mb_substr'
         12        SEND_VAR                                                 !0
         13        SEND_VAR                                                 !3
         14        SEND_VAL                                                 1
         15        SEND_VAL                                                 'UCS-4BE'
         16        DO_ICALL                                         $10     
         17        ASSIGN                                                   !4, $10
   33    18        INIT_FCALL                                               'unpack'
         19        SEND_VAL                                                 'N'
         20        SEND_VAR                                                 !4
         21        DO_ICALL                                         $12     
         22        ASSIGN                                                   !5, $12
   34    23        FETCH_DIM_R                                      ~15     !5, 1
         24        ASSIGN_DIM                                               !2
         25        OP_DATA                                                  ~15
   29    26        PRE_INC                                                  !3
         27    >   INIT_FCALL                                               'mb_strlen'
         28        SEND_VAR                                                 !0
         29        SEND_VAL                                                 'UCS-4BE'
         30        DO_ICALL                                         $17     
         31        IS_SMALLER                                               !3, $17
         32      > JMPNZ                                                    ~18, ->11
   36    33    > > RETURN                                                   !2
   37    34*     > RETURN                                                   null

End of function unistr_to_ords

Function alphabet:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 26
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 17
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 17
Branch analysis from position: 25
Branch analysis from position: 17
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 28
Branch analysis from position: 36
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 28
Branch analysis from position: 36
Branch analysis from position: 28
filename:       /in/CTWqo
function name:  alphabet
number of ops:  37
compiled vars:  !0 = $firstCharacter, !1 = $lastCharacter, !2 = $firstCharacterValue, !3 = $lastCharacterValue, !4 = $character
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV_INIT                                        !0      'A'
          1        RECV_INIT                                        !1      'Z'
          2        GENERATOR_CREATE                                         
   40     3        INIT_FCALL                                               'unistr_to_ords'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $5      
          6        FETCH_DIM_R                                      ~6      $5, 0
          7        ASSIGN                                                   !2, ~6
   41     8        INIT_FCALL                                               'unistr_to_ords'
          9        SEND_VAR                                                 !1
         10        DO_FCALL                                      0  $8      
         11        FETCH_DIM_R                                      ~9      $8, 0
         12        ASSIGN                                                   !3, ~9
   43    13        IS_SMALLER                                               !2, !3
         14      > JMPZ                                                     ~11, ->26
   44    15    >   ASSIGN                                                   !4, !2
         16      > JMP                                                      ->23
   45    17    >   INIT_FCALL                                               'ords_to_unistr'
         18        INIT_ARRAY                                       ~13     !4
         19        SEND_VAL                                                 ~13
         20        DO_FCALL                                      0  $14     
         21        YIELD                                                    $14
   44    22        PRE_INC                                                  !4
         23    >   IS_SMALLER_OR_EQUAL                                      !4, !3
         24      > JMPNZ                                                    ~17, ->17
   43    25    > > JMP                                                      ->36
   48    26    >   ASSIGN                                                   !4, !2
         27      > JMP                                                      ->34
   49    28    >   INIT_FCALL                                               'ords_to_unistr'
         29        INIT_ARRAY                                       ~19     !4
         30        SEND_VAL                                                 ~19
         31        DO_FCALL                                      0  $20     
         32        YIELD                                                    $20
   48    33        PRE_DEC                                                  !4
         34    >   IS_SMALLER_OR_EQUAL                                      !3, !4
         35      > JMPNZ                                                    ~23, ->28
   52    36    > > GENERATOR_RETURN                                         

End of function alphabet

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.21 ms | 1030 KiB | 23 Q