3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo 'Trim blank character' . "\n"; var_dump(trim('   f   ')); echo "\n\nTrim blank character via chrs function \n"; var_dump(trim_blank_chrs('   f   ')); echo "\n\nTrim normal space \n"; var_dump(trim(' f ')); /** * More robust version of PHP's trim() function. It includes a list of UTF-8 blank characters * from http://kb.mozillazine.org/Network.IDN.blacklist_chars * * @param string $string The string to trim from * @param string $charlist Optional. The stripped characters can also be specified using the charlist parameter * @return string The trimmed string */ function trim_blank_chrs($string, $charlist="") { $hex_chrs = array( 0x09 => 1, // \x{0009} 0x0A => 1, // \x{000A} 0x0B => 1, // \x{000B} 0x0D => 1, // \x{000D} 0x20 => 1, // \x{0020} 0xC2 => array(0x81 => 1, 0x8D => 1, 0x90 => 1, 0x9D => 1, 0xA0 => 1, 0xAD => 1), // \x{0081}, \x{008D}, \x{0090}, \x{009D}, \x{00A0}, \x{00AD} 0xCC => array(0xB7 => 1, 0xB8 => 1), // \x{0337}, \x{0338} 0xE1 => array(0x85 => array(0x9F => 1, 0xA0 => 1), 0x9A => array(0x80 => 1), 0xA0 => array(0x8E => 1)), // \x{115F}, \x{1160}, \x{1680}, \x{180E} 0xE2 => array(0x80 => array(0x80 => 1, 0x81 => 1, 0x82 => 1, 0x83 => 1, 0x84 => 1, 0x85 => 1, 0x86 => 1, 0x87 => 1, 0x88 => 1, 0x89 => 1, 0x8A => 1, 0x8B => 1, 0x8C => 1, 0x8D => 1, 0x8E => 1, 0x8F => 1, // \x{2000} - \x{200F} 0xA8 => 1, 0xA9 => 1, 0xAA => 1, 0xAB => 1, 0xAC => 1, 0xAD => 1, 0xAE => 1, 0xAF => 1), // \x{2028} - \x{202F} 0x81 => array(0x9F => 1)), // \x{205F} 0xE3 => array(0x80 => array(0x80 => 1), // \x{3000} 0x85 => array(0xA4 => 1)), // \x{3164} 0xEF => array(0xBB => array(0xBF => 1), // \x{FEFF} 0xBE => array(0xA0 => 1), // \x{FFA0} 0xBF => array(0xB9 => 1, 0xBA => 1, 0xBB => 1)), // \x{FFF9} - \x{FFFB} ); $hex_chrs_rev = array( 0x09 => 1, // \x{0009} 0x0A => 1, // \x{000A} 0x0B => 1, // \x{000B} 0x0D => 1, // \x{000D} 0x20 => 1, // \x{0020} 0x81 => array(0xC2 => 1, 0x80 => array(0xE2 => 1)), // \x{0081}, \x{2001} 0x8D => array(0xC2 => 1, 0x80 => array(0xE2 => 1)), // \x{008D}, \x{200D} 0x90 => array(0xC2 => 1), // \x{0090} 0x9D => array(0xC2 => 1), // \x{009D} 0xA0 => array(0xC2 => 1, 0x85 => array(0xE1 => 1), 0x81 => array(0xE2 => 1), 0xBE => array(0xEF => 1)), // \x{00A0}, \x{1160}, \x{2060}, \x{FFA0} 0xAD => array(0xC2 => 1, 0x80 => array(0xE2 => 1)), // \x{00AD}, \x{202D} 0xB8 => array(0xCC => 1), // \x{0338} 0xB7 => array(0xCC => 1), // \x{0337} 0x9F => array(0x85 => array(0xE1 => 1), 0x81 => array(0xE2 => 1)), // \x{115F}, \x{205F} 0x80 => array(0x9A => array(0xE1 => 1), 0x80 => array(0xE2 => 1, 0xE3 => 1)), // \x{1680}, \x{2000}, \x{3000} 0x8E => array(0xA0 => array(0xE1 => 1), 0x80 => array(0xE2 => 1)), // \x{180E}, \x{200E} 0x82 => array(0x80 => array(0xE2 => 1)), // \x{2002} 0x83 => array(0x80 => array(0xE2 => 1)), // \x{2003} 0x84 => array(0x80 => array(0xE2 => 1)), // \x{2004} 0x85 => array(0x80 => array(0xE2 => 1)), // \x{2005} 0x86 => array(0x80 => array(0xE2 => 1)), // \x{2006} 0x87 => array(0x80 => array(0xE2 => 1)), // \x{2007} 0x88 => array(0x80 => array(0xE2 => 1)), // \x{2008} 0x89 => array(0x80 => array(0xE2 => 1)), // \x{2009} 0x8A => array(0x80 => array(0xE2 => 1)), // \x{200A} 0x8B => array(0x80 => array(0xE2 => 1)), // \x{200B} 0x8C => array(0x80 => array(0xE2 => 1)), // \x{200C} 0x8F => array(0x80 => array(0xE2 => 1)), // \x{200F} 0xA8 => array(0x80 => array(0xE2 => 1)), // \x{2028} 0xA9 => array(0x80 => array(0xE2 => 1)), // \x{2029} 0xAA => array(0x80 => array(0xE2 => 1)), // \x{202A} 0xAB => array(0x80 => array(0xE2 => 1)), // \x{202B} 0xAC => array(0x80 => array(0xE2 => 1)), // \x{202C} 0xAE => array(0x80 => array(0xE2 => 1)), // \x{202E} 0xAF => array(0x80 => array(0xE2 => 1)), // \x{202F} 0xA4 => array(0x85 => array(0xE3 => 1)), // \x{3164} 0xBF => array(0xBB => array(0xEF => 1)), // \x{FEFF} 0xB9 => array(0xBF => array(0xEF => 1)), // \x{FFF9} 0xBA => array(0xBF => array(0xEF => 1)), // \x{FFFA} 0xBB => array(0xBF => array(0xEF => 1)), // \x{FFFB} ); // Start from the beginning and work our way in $i = 0; do { // Check to see if we have matched a first character in our utf-8 array $offset = match_sequence($string, $hex_chrs); if(!$offset) { // If not, then we must have a "good" character and we don't need to do anymore processing break; } $string = substr($string, $offset); } while(++$i); // Start from the end and work our way in $string = strrev($string); $i = 0; do { // Check to see if we have matched a first character in our utf-8 array $offset = match_sequence($string, $hex_chrs_rev); if(!$offset) { // If not, then we must have a "good" character and we don't need to do anymore processing break; } $string = substr($string, $offset); } while(++$i); $string = strrev($string); if($charlist) { $string = trim($string, $charlist); } else { $string = trim($string); } return $string; } /** * Match a sequence * * @param string $string The string to match from * @param array $array The array to match from * @param int $i Number in the string * @param int $n Number of matches * @return int The number matched */ function match_sequence($string, $array, $i=0, $n=0) { if($string === "") { return 0; } $ord = ord($string[$i]); if(array_key_exists($ord, $array)) { $level = $array[$ord]; ++$n; if(is_array($level)) { ++$i; return match_sequence($string, $level, $i, $n); } return $n; } return 0; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ObKdY
function name:  (null)
number of ops:  22
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ECHO                                                     'Trim+blank+character%0A'
    4     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'trim'
          3        SEND_VAL                                                 '%E2%80%AF%E2%80%AF%E2%80%AFf%E2%80%AF%E2%80%AF%E2%80%AF'
          4        DO_ICALL                                         $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
    6     7        ECHO                                                     '%0A%0ATrim+blank+character+via+chrs+function+%0A'
    7     8        INIT_FCALL                                               'var_dump'
          9        INIT_FCALL_BY_NAME                                       'trim_blank_chrs'
         10        SEND_VAL_EX                                              '%E2%80%AF%E2%80%AF%E2%80%AFf%E2%80%AF%E2%80%AF%E2%80%AF'
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
    9    14        ECHO                                                     '%0A%0ATrim+normal+space+%0A'
   10    15        INIT_FCALL                                               'var_dump'
         16        INIT_FCALL                                               'trim'
         17        SEND_VAL                                                 '++f++'
         18        DO_ICALL                                         $4      
         19        SEND_VAR                                                 $4
         20        DO_ICALL                                                 
  158    21      > RETURN                                                   1

Function trim_blank_chrs:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 33
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 51
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 25
Branch analysis from position: 40
Branch analysis from position: 25
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 5
Branch analysis from position: 20
Branch analysis from position: 5
filename:       /in/ObKdY
function name:  trim_blank_chrs
number of ops:  57
compiled vars:  !0 = $string, !1 = $charlist, !2 = $hex_chrs, !3 = $hex_chrs_rev, !4 = $i, !5 = $offset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
   22     2        ASSIGN                                                   !2, <array>
   41     3        ASSIGN                                                   !3, <array>
   85     4        ASSIGN                                                   !4, 0
   89     5    >   INIT_FCALL_BY_NAME                                       'match_sequence'
          6        SEND_VAR_EX                                              !0
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0  $9      
          9        ASSIGN                                                   !5, $9
   90    10        BOOL_NOT                                         ~11     !5
         11      > JMPZ                                                     ~11, ->13
   93    12    > > JMP                                                      ->20
   95    13    >   INIT_FCALL                                               'substr'
         14        SEND_VAR                                                 !0
         15        SEND_VAR                                                 !5
         16        DO_ICALL                                         $12     
         17        ASSIGN                                                   !0, $12
   97    18        PRE_INC                                          ~14     !4
         19      > JMPNZ                                                    ~14, ->5
  100    20    >   INIT_FCALL                                               'strrev'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $15     
         23        ASSIGN                                                   !0, $15
  101    24        ASSIGN                                                   !4, 0
  105    25    >   INIT_FCALL_BY_NAME                                       'match_sequence'
         26        SEND_VAR_EX                                              !0
         27        SEND_VAR_EX                                              !3
         28        DO_FCALL                                      0  $18     
         29        ASSIGN                                                   !5, $18
  106    30        BOOL_NOT                                         ~20     !5
         31      > JMPZ                                                     ~20, ->33
  109    32    > > JMP                                                      ->40
  111    33    >   INIT_FCALL                                               'substr'
         34        SEND_VAR                                                 !0
         35        SEND_VAR                                                 !5
         36        DO_ICALL                                         $21     
         37        ASSIGN                                                   !0, $21
  113    38        PRE_INC                                          ~23     !4
         39      > JMPNZ                                                    ~23, ->25
  114    40    >   INIT_FCALL                                               'strrev'
         41        SEND_VAR                                                 !0
         42        DO_ICALL                                         $24     
         43        ASSIGN                                                   !0, $24
  116    44      > JMPZ                                                     !1, ->51
  118    45    >   INIT_FCALL                                               'trim'
         46        SEND_VAR                                                 !0
         47        SEND_VAR                                                 !1
         48        DO_ICALL                                         $26     
         49        ASSIGN                                                   !0, $26
  116    50      > JMP                                                      ->55
  122    51    >   INIT_FCALL                                               'trim'
         52        SEND_VAR                                                 !0
         53        DO_ICALL                                         $28     
         54        ASSIGN                                                   !0, $28
  125    55    > > RETURN                                                   !0
  126    56*     > RETURN                                                   null

End of function trim_blank_chrs

Function match_sequence:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 28
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 27
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ObKdY
function name:  match_sequence
number of ops:  30
compiled vars:  !0 = $string, !1 = $array, !2 = $i, !3 = $n, !4 = $ord, !5 = $level
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  137     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      0
          3        RECV_INIT                                        !3      0
  139     4        IS_IDENTICAL                                             !0, ''
          5      > JMPZ                                                     ~6, ->7
  141     6    > > RETURN                                                   0
  144     7    >   INIT_FCALL                                               'ord'
          8        FETCH_DIM_R                                      ~7      !0, !2
          9        SEND_VAL                                                 ~7
         10        DO_ICALL                                         $8      
         11        ASSIGN                                                   !4, $8
  145    12        ARRAY_KEY_EXISTS                                         !4, !1
         13      > JMPZ                                                     ~10, ->28
  147    14    >   FETCH_DIM_R                                      ~11     !1, !4
         15        ASSIGN                                                   !5, ~11
  148    16        PRE_INC                                                  !3
  149    17        TYPE_CHECK                                  128          !5
         18      > JMPZ                                                     ~14, ->27
  151    19    >   PRE_INC                                                  !2
  152    20        INIT_FCALL_BY_NAME                                       'match_sequence'
         21        SEND_VAR_EX                                              !0
         22        SEND_VAR_EX                                              !5
         23        SEND_VAR_EX                                              !2
         24        SEND_VAR_EX                                              !3
         25        DO_FCALL                                      0  $16     
         26      > RETURN                                                   $16
  154    27    > > RETURN                                                   !3
  157    28    > > RETURN                                                   0
  158    29*     > RETURN                                                   null

End of function match_sequence

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.32 ms | 1023 KiB | 18 Q