3v4l.org

run code in 300+ PHP versions simultaneously
<?php class KeywordParameters { public $post_name; public $cat_slug; public $base; public $medium; public function __construct($post_name, $cat_slug, $base, $medium) { $this->post_name = $post_name; $this->cat_slug = $cat_slug; $this->base = $base; $this->medium = $medium; } } class Replacements { public $replacements_list; public $keyword_parameters; public function __construct($replacements_list, $keyword_parameters) { $this->replacements_list = $replacements_list; $this->keyword_parameters = $keyword_parameters; } } class ReplacementTasks { const PATTERN_FORMAT = '~(?=[%s])(?=\b\w|(?<!\S)\W)(?:%s)(?<=\w\b|\W(?!\S))~ui'; protected $replacement_tasks = []; protected $formats = []; protected $pattern = false; protected $first_characters; protected $first_character_discrimination; protected $alternation; protected $keywords = []; public function add(Replacements $replacements) { $params = $replacements->keyword_parameters; $format = '<a href="' . $params->base . '%s?utm_source=blogpost&utm_medium=' . $params->medium . '&utm_content=' . $params->post_name . '&utm_campaign=' . $params->cat_slug . '" target="_blank">%s</a>'; $formats_last_index = array_push($this->formats, $format) - 1; foreach ($replacements->replacements_list as $keyword => $replacement) { $this->first_characters[] = mb_substr($keyword, 0, 1, 'UTF-8'); $this->replacement_tasks[$keyword] = [ 'replacement' => $replacement, 'format' => &$this->formats[$formats_last_index] ]; $this->keywords[] = preg_quote($keyword, '~'); } $this->pattern = false; } public function replace($str) { $this->build_pattern(); $that = &$this; return preg_replace_callback($this->pattern, function ($m) use (&$that) { return sprintf($that->replacement_tasks[$m[0]]['format'], $that->replacement_tasks[$m[0]]['replacement'], $m[0]); }, $str); } protected function build_pattern() { if ( $this->pattern ) return; $this->build_pattern_elements(); $this->pattern = sprintf(self::PATTERN_FORMAT, $this->first_character_discrimination, $this->alternation); } protected function build_pattern_elements() { rsort($this->keywords); $this->alternation = implode('|', $this->keywords); $this->first_character_discrimination = preg_quote(implode('', array_unique($this->first_characters)), '~'); } } $data = array( 'post_content' => 'làm game developer việc làm Game Developer việc làm Game Developer TPHCM việc làm Game Developer Hà Nội việc làm Senior Game Developer việc làm Senior Game Developer TPHCM', 'post_name' => 'Test post' ); $cat_slug = 'test'; $parameters_external = new KeywordParameters($data['post_name'], $cat_slug, 'https://itviec.com/', 'jobs'); $parameters_internal = new KeywordParameters($data['post_name'], $cat_slug, 'https://blog.itviec.com/', 'referral'); $keywords_internal = array( "làm game developer" => "game-developer-ban-co-muon-lam/", ); $keywords_external = array( "việc làm Game Developer TPHCM" => "viec-lam-it/game-developer/ho-chi-minh-hcm", "việc làm Game Developer Hà Nội" => "viec-lam-it/game-developer/ha-noi", "việc làm Game Developer" => "viec-lam-it/game-developer", ); $replacements_external = new Replacements($keywords_external, $parameters_external); $replacements_internal = new Replacements($keywords_internal, $parameters_internal); $replacement_tasks = new ReplacementTasks(); $replacement_tasks->add($replacements_external); $replacement_tasks->add($replacements_internal); echo $replacement_tasks->replace($data['post_content']);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GQJ5l
function name:  (null)
number of ops:  48
compiled vars:  !0 = $data, !1 = $cat_slug, !2 = $parameters_external, !3 = $parameters_internal, !4 = $keywords_internal, !5 = $keywords_external, !6 = $replacements_external, !7 = $replacements_internal, !8 = $replacement_tasks
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   ASSIGN                                                   !0, <array>
   85     1        ASSIGN                                                   !1, 'test'
   87     2        NEW                                              $11     'KeywordParameters'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_DIM_FUNC_ARG                               $12     !0, 'post_name'
          5        SEND_FUNC_ARG                                            $12
          6        SEND_VAR_EX                                              !1
          7        SEND_VAL_EX                                              'https%3A%2F%2Fitviec.com%2F'
          8        SEND_VAL_EX                                              'jobs'
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $11
   88    11        NEW                                              $15     'KeywordParameters'
         12        CHECK_FUNC_ARG                                           
         13        FETCH_DIM_FUNC_ARG                               $16     !0, 'post_name'
         14        SEND_FUNC_ARG                                            $16
         15        SEND_VAR_EX                                              !1
         16        SEND_VAL_EX                                              'https%3A%2F%2Fblog.itviec.com%2F'
         17        SEND_VAL_EX                                              'referral'
         18        DO_FCALL                                      0          
         19        ASSIGN                                                   !3, $15
   90    20        ASSIGN                                                   !4, <array>
   94    21        ASSIGN                                                   !5, <array>
  100    22        NEW                                              $21     'Replacements'
         23        SEND_VAR_EX                                              !5
         24        SEND_VAR_EX                                              !2
         25        DO_FCALL                                      0          
         26        ASSIGN                                                   !6, $21
  101    27        NEW                                              $24     'Replacements'
         28        SEND_VAR_EX                                              !4
         29        SEND_VAR_EX                                              !3
         30        DO_FCALL                                      0          
         31        ASSIGN                                                   !7, $24
  103    32        NEW                                              $27     'ReplacementTasks'
         33        DO_FCALL                                      0          
         34        ASSIGN                                                   !8, $27
  104    35        INIT_METHOD_CALL                                         !8, 'add'
         36        SEND_VAR_EX                                              !6
         37        DO_FCALL                                      0          
  105    38        INIT_METHOD_CALL                                         !8, 'add'
         39        SEND_VAR_EX                                              !7
         40        DO_FCALL                                      0          
  107    41        INIT_METHOD_CALL                                         !8, 'replace'
         42        CHECK_FUNC_ARG                                           
         43        FETCH_DIM_FUNC_ARG                               $32     !0, 'post_content'
         44        SEND_FUNC_ARG                                            $32
         45        DO_FCALL                                      0  $33     
         46        ECHO                                                     $33
         47      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FGQJ5l%3A62%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GQJ5l
function name:  {closure}
number of ops:  18
compiled vars:  !0 = $m, !1 = $that
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   63     2        INIT_FCALL                                               'sprintf'
          3        FETCH_DIM_R                                      ~3      !0, 0
          4        FETCH_OBJ_R                                      ~2      !1, 'replacement_tasks'
          5        FETCH_DIM_R                                      ~4      ~2, ~3
          6        FETCH_DIM_R                                      ~5      ~4, 'format'
          7        SEND_VAL                                                 ~5
          8        FETCH_DIM_R                                      ~7      !0, 0
          9        FETCH_OBJ_R                                      ~6      !1, 'replacement_tasks'
         10        FETCH_DIM_R                                      ~8      ~6, ~7
         11        FETCH_DIM_R                                      ~9      ~8, 'replacement'
         12        SEND_VAL                                                 ~9
         13        FETCH_DIM_R                                      ~10     !0, 0
         14        SEND_VAL                                                 ~10
         15        DO_ICALL                                         $11     
         16      > RETURN                                                   $11
   64    17*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FGQJ5l%3A62%240

Class KeywordParameters:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GQJ5l
function name:  __construct
number of ops:  13
compiled vars:  !0 = $post_name, !1 = $cat_slug, !2 = $base, !3 = $medium
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
    9     4        ASSIGN_OBJ                                               'post_name'
          5        OP_DATA                                                  !0
   10     6        ASSIGN_OBJ                                               'cat_slug'
          7        OP_DATA                                                  !1
   11     8        ASSIGN_OBJ                                               'base'
          9        OP_DATA                                                  !2
   12    10        ASSIGN_OBJ                                               'medium'
         11        OP_DATA                                                  !3
   13    12      > RETURN                                                   null

End of function __construct

End of class KeywordParameters.

Class Replacements:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GQJ5l
function name:  __construct
number of ops:  7
compiled vars:  !0 = $replacements_list, !1 = $keyword_parameters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   21     2        ASSIGN_OBJ                                               'replacements_list'
          3        OP_DATA                                                  !0
   22     4        ASSIGN_OBJ                                               'keyword_parameters'
          5        OP_DATA                                                  !1
   23     6      > RETURN                                                   null

End of function __construct

End of class Replacements.

Class ReplacementTasks:
Function add:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 51
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 51
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
filename:       /in/GQJ5l
function name:  add
number of ops:  55
compiled vars:  !0 = $replacements, !1 = $params, !2 = $format, !3 = $formats_last_index, !4 = $replacement, !5 = $keyword
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        FETCH_OBJ_R                                      ~6      !0, 'keyword_parameters'
          2        ASSIGN                                                   !1, ~6
   42     3        FETCH_OBJ_R                                      ~8      !1, 'base'
          4        CONCAT                                           ~9      '%3Ca+href%3D%22', ~8
          5        CONCAT                                           ~10     ~9, '%25s%3Futm_source%3Dblogpost%26utm_medium%3D'
          6        FETCH_OBJ_R                                      ~11     !1, 'medium'
          7        CONCAT                                           ~12     ~10, ~11
   43     8        CONCAT                                           ~13     ~12, '%26utm_content%3D'
          9        FETCH_OBJ_R                                      ~14     !1, 'post_name'
         10        CONCAT                                           ~15     ~13, ~14
         11        CONCAT                                           ~16     ~15, '%26utm_campaign%3D'
         12        FETCH_OBJ_R                                      ~17     !1, 'cat_slug'
         13        CONCAT                                           ~18     ~16, ~17
   44    14        CONCAT                                           ~19     ~18, '%22+target%3D%22_blank%22%3E%25s%3C%2Fa%3E'
   42    15        ASSIGN                                                   !2, ~19
   45    16        INIT_FCALL                                               'array_push'
         17        FETCH_OBJ_W                                      $21     'formats'
         18        SEND_REF                                                 $21
         19        SEND_VAR                                                 !2
         20        DO_ICALL                                         $22     
         21        SUB                                              ~23     $22, 1
         22        ASSIGN                                                   !3, ~23
   47    23        FETCH_OBJ_R                                      ~25     !0, 'replacements_list'
         24      > FE_RESET_R                                       $26     ~25, ->51
         25    > > FE_FETCH_R                                       ~27     $26, !4, ->51
         26    >   ASSIGN                                                   !5, ~27
   48    27        INIT_FCALL                                               'mb_substr'
         28        SEND_VAR                                                 !5
         29        SEND_VAL                                                 0
         30        SEND_VAL                                                 1
         31        SEND_VAL                                                 'UTF-8'
         32        DO_ICALL                                         $31     
         33        FETCH_OBJ_W                                      $29     'first_characters'
         34        ASSIGN_DIM                                               $29
         35        OP_DATA                                                  $31
   50    36        INIT_ARRAY                                       ~34     !4, 'replacement'
   51    37        FETCH_OBJ_W                                      $35     'formats'
         38        FETCH_DIM_W                                      $36     $35, !3
         39        ADD_ARRAY_ELEMENT                                ~34     $36, 'format'
   49    40        FETCH_OBJ_W                                      $32     'replacement_tasks'
         41        ASSIGN_DIM                                               $32, !5
   51    42        OP_DATA                                                  ~34
   53    43        INIT_FCALL                                               'preg_quote'
         44        SEND_VAR                                                 !5
         45        SEND_VAL                                                 '%7E'
         46        DO_ICALL                                         $39     
         47        FETCH_OBJ_W                                      $37     'keywords'
         48        ASSIGN_DIM                                               $37
         49        OP_DATA                                                  $39
   47    50      > JMP                                                      ->25
         51    >   FE_FREE                                                  $26
   56    52        ASSIGN_OBJ                                               'pattern'
         53        OP_DATA                                                  <false>
   57    54      > RETURN                                                   null

End of function add

Function replace:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GQJ5l
function name:  replace
number of ops:  15
compiled vars:  !0 = $str, !1 = $that
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   60     1        INIT_METHOD_CALL                                         'build_pattern'
          2        DO_FCALL                                      0          
   61     3        FETCH_THIS                                       $3      
          4        ASSIGN_REF                                               !1, $3
   62     5        INIT_FCALL                                               'preg_replace_callback'
          6        FETCH_OBJ_R                                      ~5      'pattern'
          7        SEND_VAL                                                 ~5
          8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FGQJ5l%3A62%240'
          9        BIND_LEXICAL                                             ~6, !1
   64    10        SEND_VAL                                                 ~6
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $7      
         13      > RETURN                                                   $7
   65    14*     > RETURN                                                   null

End of function replace

Function build_pattern:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 3
Branch analysis from position: 2
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GQJ5l
function name:  build_pattern
number of ops:  15
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   FETCH_OBJ_R                                      ~0      'pattern'
          1      > JMPZ                                                     ~0, ->3
          2    > > RETURN                                                   null
   69     3    >   INIT_METHOD_CALL                                         'build_pattern_elements'
          4        DO_FCALL                                      0          
   70     5        INIT_FCALL                                               'sprintf'
          6        SEND_VAL                                                 '%7E%28%3F%3D%5B%25s%5D%29%28%3F%3D%5Cb%5Cw%7C%28%3F%3C%21%5CS%29%5CW%29%28%3F%3A%25s%29%28%3F%3C%3D%5Cw%5Cb%7C%5CW%28%3F%21%5CS%29%29%7Eui'
          7        FETCH_OBJ_R                                      ~3      'first_character_discrimination'
          8        SEND_VAL                                                 ~3
          9        FETCH_OBJ_R                                      ~4      'alternation'
         10        SEND_VAL                                                 ~4
         11        DO_ICALL                                         $5      
         12        ASSIGN_OBJ                                               'pattern'
         13        OP_DATA                                                  $5
   71    14      > RETURN                                                   null

End of function build_pattern

Function build_pattern_elements:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GQJ5l
function name:  build_pattern_elements
number of ops:  26
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   INIT_FCALL                                               'rsort'
          1        FETCH_OBJ_W                                      $0      'keywords'
          2        SEND_REF                                                 $0
          3        DO_ICALL                                                 
   75     4        INIT_FCALL                                               'implode'
          5        SEND_VAL                                                 '%7C'
          6        FETCH_OBJ_R                                      ~3      'keywords'
          7        SEND_VAL                                                 ~3
          8        DO_ICALL                                         $4      
          9        ASSIGN_OBJ                                               'alternation'
         10        OP_DATA                                                  $4
   76    11        INIT_FCALL                                               'preg_quote'
         12        INIT_FCALL                                               'implode'
         13        SEND_VAL                                                 ''
         14        INIT_FCALL                                               'array_unique'
         15        FETCH_OBJ_R                                      ~6      'first_characters'
         16        SEND_VAL                                                 ~6
         17        DO_ICALL                                         $7      
         18        SEND_VAR                                                 $7
         19        DO_ICALL                                         $8      
         20        SEND_VAR                                                 $8
         21        SEND_VAL                                                 '%7E'
         22        DO_ICALL                                         $9      
         23        ASSIGN_OBJ                                               'first_character_discrimination'
         24        OP_DATA                                                  $9
   77    25      > RETURN                                                   null

End of function build_pattern_elements

End of class ReplacementTasks.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.99 ms | 1416 KiB | 29 Q