3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Zefram_Filter_StringTruncate implements Zend_Filter_Interface { /** * @var int */ protected $_length = 80; /** * @var string */ protected $_ending = '...'; /** * @var bool */ protected $_breakWords = false; /** * @var string */ protected $_charset = 'utf-8'; /** * @param array|Traversable $options OPTIONAL */ public function __construct($options = null) { if ($options) { foreach ($options as $key => $value) { $method = 'set' . $key; if (method_exists($this, $method)) { $this->$method($value); } } } } /** * @param int $length * @return Zefram_Filter_StringTruncate */ public function setLength($length) { $this->_length = (int) $length; return $this; } /** * @return int */ public function getLength() { return $this->_length; } /** * @param string $ending * @return Zefram_Filter_StringTruncate */ public function setEnding($ending) { $this->_ending = (string) $ending; return $this; } /** * @return string */ public function getEnding($ending) { return $this->_ending; } /** * @param bool $breakWords * @return Zefram_Filter_StringTruncate */ public function setBreakWords($breakWords) { $this->_breakWords = (bool) $breakWords; return $this; } /** * @return bool */ public function getBreakWords() { return $this->_breakWords; } /** * @param string $charset * @return Zefram_Filter_StringTruncate */ public function setCharset($charset) { $this->_charset = (string) $charset; return $this; } /** * @return string */ public function getCharset() { return $this->_charset; } /** * @param string $value * @return string */ public function filter($value) { return self::stringTruncate( $value, $this->getLength(), $this->getEnding(), $this->getBreakWords(), $this->getCharset() ); } /** * @param string $string * @param int $length * @param string $more * @param bool $breakWords * @param string $enc * @return string */ public static function stringTruncate($string, $length = 80, $more = '...', $breakWords = false, $enc = 'utf-8') { $length = (int) $length; if ($length <= 0) { return ''; } // trim input string $string = (string) $string; $string = preg_replace('/(^\s+)|(\s+$)/u', '', $string); if (mb_strlen($string, $enc) > $length) { // make room for postfix $length -= min($length, mb_strlen($more, $enc)); // do not break words, truncate string at last whitespace // found between 0 and $length index if (!$breakWords) { $string = preg_replace('/\s+(\S+)?$/u', '', mb_substr($string, 0, $length + 1)); // One of two scenarios took place: // - dangling word preceded by whitespace was truncated, or // - string is intact, as it consists of a single word // length+1 jest na wypadek, gdyby ostatni wyraz konczyl sie na granicznej // pozycji. W przeciwnym razie nie byłby uwzględniony w wyniku. Przykładowo: // $string = 'a_bc_'; // obcinamy do 4 znakow -> bez length+1 dostajemy 'a', z length+1 dostajemy 'a_bc' } return mb_substr($string, 0, $length, $enc) . $more; } return $string; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  (null)
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'zefram_filter_stringtruncate'
  169     1      > RETURN                                                   1

Class Zefram_Filter_StringTruncate:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 18
Branch analysis from position: 2
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 16
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
Branch analysis from position: 18
filename:       /in/UDEYo
function name:  __construct
number of ops:  19
compiled vars:  !0 = $options, !1 = $value, !2 = $key, !3 = $method
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV_INIT                                        !0      null
   30     1      > JMPZ                                                     !0, ->18
   31     2    > > FE_RESET_R                                       $4      !0, ->17
          3    > > FE_FETCH_R                                       ~5      $4, !1, ->17
          4    >   ASSIGN                                                   !2, ~5
   32     5        CONCAT                                           ~7      'set', !2
          6        ASSIGN                                                   !3, ~7
   33     7        INIT_FCALL                                               'method_exists'
          8        FETCH_THIS                                       ~9      
          9        SEND_VAL                                                 ~9
         10        SEND_VAR                                                 !3
         11        DO_ICALL                                         $10     
         12      > JMPZ                                                     $10, ->16
   34    13    >   INIT_METHOD_CALL                                         !3
         14        SEND_VAR_EX                                              !1
         15        DO_FCALL                                      0          
   31    16    > > JMP                                                      ->3
         17    >   FE_FREE                                                  $4
   38    18    > > RETURN                                                   null

End of function __construct

Function setlength:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  setLength
number of ops:  7
compiled vars:  !0 = $length
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        CAST                                          4  ~2      !0
          2        ASSIGN_OBJ                                               '_length'
          3        OP_DATA                                                  ~2
   47     4        FETCH_THIS                                       ~3      
          5      > RETURN                                                   ~3
   48     6*     > RETURN                                                   null

End of function setlength

Function getlength:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  getLength
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   FETCH_OBJ_R                                      ~0      '_length'
          1      > RETURN                                                   ~0
   56     2*     > RETURN                                                   null

End of function getlength

Function setending:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  setEnding
number of ops:  7
compiled vars:  !0 = $ending
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
   64     1        CAST                                          6  ~2      !0
          2        ASSIGN_OBJ                                               '_ending'
          3        OP_DATA                                                  ~2
   65     4        FETCH_THIS                                       ~3      
          5      > RETURN                                                   ~3
   66     6*     > RETURN                                                   null

End of function setending

Function getending:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  getEnding
number of ops:  4
compiled vars:  !0 = $ending
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
   73     1        FETCH_OBJ_R                                      ~1      '_ending'
          2      > RETURN                                                   ~1
   74     3*     > RETURN                                                   null

End of function getending

Function setbreakwords:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  setBreakWords
number of ops:  7
compiled vars:  !0 = $breakWords
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
   82     1        BOOL                                             ~2      !0
          2        ASSIGN_OBJ                                               '_breakWords'
          3        OP_DATA                                                  ~2
   83     4        FETCH_THIS                                       ~3      
          5      > RETURN                                                   ~3
   84     6*     > RETURN                                                   null

End of function setbreakwords

Function getbreakwords:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  getBreakWords
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   FETCH_OBJ_R                                      ~0      '_breakWords'
          1      > RETURN                                                   ~0
   92     2*     > RETURN                                                   null

End of function getbreakwords

Function setcharset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  setCharset
number of ops:  7
compiled vars:  !0 = $charset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   RECV                                             !0      
  100     1        CAST                                          6  ~2      !0
          2        ASSIGN_OBJ                                               '_charset'
          3        OP_DATA                                                  ~2
  101     4        FETCH_THIS                                       ~3      
          5      > RETURN                                                   ~3
  102     6*     > RETURN                                                   null

End of function setcharset

Function getcharset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  getCharset
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  109     0  E >   FETCH_OBJ_R                                      ~0      '_charset'
          1      > RETURN                                                   ~0
  110     2*     > RETURN                                                   null

End of function getcharset

Function filter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  filter
number of ops:  18
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  116     0  E >   RECV                                             !0      
  118     1        INIT_STATIC_METHOD_CALL                                  'stringTruncate'
  119     2        SEND_VAR_EX                                              !0
  120     3        INIT_METHOD_CALL                                         'getLength'
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR_NO_REF_EX                                       $1
  121     6        INIT_METHOD_CALL                                         'getEnding'
          7        DO_FCALL                                      0  $2      
          8        SEND_VAR_NO_REF_EX                                       $2
  122     9        INIT_METHOD_CALL                                         'getBreakWords'
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR_NO_REF_EX                                       $3
  123    12        INIT_METHOD_CALL                                         'getCharset'
         13        DO_FCALL                                      0  $4      
         14        SEND_VAR_NO_REF_EX                                       $4
         15        DO_FCALL                                      0  $5      
         16      > RETURN                                                   $5
  125    17*     > RETURN                                                   null

End of function filter

Function stringtruncate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 55
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 47
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UDEYo
function name:  stringTruncate
number of ops:  57
compiled vars:  !0 = $string, !1 = $length, !2 = $more, !3 = $breakWords, !4 = $enc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  135     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      80
          2        RECV_INIT                                        !2      '...'
          3        RECV_INIT                                        !3      <false>
          4        RECV_INIT                                        !4      'utf-8'
  137     5        CAST                                          4  ~5      !1
          6        ASSIGN                                                   !1, ~5
  139     7        IS_SMALLER_OR_EQUAL                                      !1, 0
          8      > JMPZ                                                     ~7, ->10
  140     9    > > RETURN                                                   ''
  144    10    >   CAST                                          6  ~8      !0
         11        ASSIGN                                                   !0, ~8
  145    12        INIT_FCALL                                               'preg_replace'
         13        SEND_VAL                                                 '%2F%28%5E%5Cs%2B%29%7C%28%5Cs%2B%24%29%2Fu'
         14        SEND_VAL                                                 ''
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $10     
         17        ASSIGN                                                   !0, $10
  147    18        INIT_FCALL                                               'mb_strlen'
         19        SEND_VAR                                                 !0
         20        SEND_VAR                                                 !4
         21        DO_ICALL                                         $12     
         22        IS_SMALLER                                               !1, $12
         23      > JMPZ                                                     ~13, ->55
  149    24    >   INIT_FCALL                                               'min'
         25        SEND_VAR                                                 !1
         26        INIT_FCALL                                               'mb_strlen'
         27        SEND_VAR                                                 !2
         28        SEND_VAR                                                 !4
         29        DO_ICALL                                         $14     
         30        SEND_VAR                                                 $14
         31        DO_ICALL                                         $15     
         32        ASSIGN_OP                                     2          !1, $15
  153    33        BOOL_NOT                                         ~17     !3
         34      > JMPZ                                                     ~17, ->47
  154    35    >   INIT_FCALL                                               'preg_replace'
         36        SEND_VAL                                                 '%2F%5Cs%2B%28%5CS%2B%29%3F%24%2Fu'
         37        SEND_VAL                                                 ''
         38        INIT_FCALL                                               'mb_substr'
         39        SEND_VAR                                                 !0
         40        SEND_VAL                                                 0
         41        ADD                                              ~18     !1, 1
         42        SEND_VAL                                                 ~18
         43        DO_ICALL                                         $19     
         44        SEND_VAR                                                 $19
         45        DO_ICALL                                         $20     
         46        ASSIGN                                                   !0, $20
  164    47    >   INIT_FCALL                                               'mb_substr'
         48        SEND_VAR                                                 !0
         49        SEND_VAL                                                 0
         50        SEND_VAR                                                 !1
         51        SEND_VAR                                                 !4
         52        DO_ICALL                                         $22     
         53        CONCAT                                           ~23     $22, !2
         54      > RETURN                                                   ~23
  167    55    > > RETURN                                                   !0
  168    56*     > RETURN                                                   null

End of function stringtruncate

End of class Zefram_Filter_StringTruncate.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.32 ms | 1412 KiB | 23 Q