3v4l.org

run code in 300+ PHP versions simultaneously
<?php $desc = '<p> <strong>Nappe bouton power</strong><br> <u class="uuu"><i title="a bouton power b">Nappe bouton power, compatible Samsung Galaxy S6 Edge G925F.</i></u> </p>'; echo canLinkThisWordIn('bouton power', $desc, true); function canLinkThisWordIn($word, $html, $linkIt){ $forbidenTagsArray = array('strong', 'a', 'b', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'); $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXpath($dom); $wordTags = $xpath->query("//*[text()[contains(translate(., '".strtoupper($word)."', '".strtolower($word)."'), '".strtolower($word)."')]]"); foreach($wordTags as $wordTag){ if(checkTagAndParent($wordTag, $forbidenTagsArray)){ if($linkIt){ //$newWordTagValue = ''; //$wordTag->nodeValue = ''; //$wordTag->appendChild($dom->createTextNode($titleText)); //$replacement = $dom->createDocumentFragment(); //$replacement->appendChild($newWordTag); $sensitiveWord = $word; $newWordTagLink = '<a href="#" title="#" class="internalink"><b>'.$sensitiveWord.'</b></a>'; $newWordTagHtml = str_ireplace($sensitiveWord, $newWordTagLink, getNodeInnerHTML($wordTag)); $helper = new DOMDocument(); $helper->loadHTML($newWordTagHtml); $newWordTag = $dom->createElement('b'); $newWordTag->appendChild($dom->importNode($helper->documentElement, true)); // save to dom $wordTag->parentNode->replaceChild($newWordTag, $wordTag); $body = $dom->getElementsByTagName('body'); $body = $body->item(0); $newHTML = $dom->saveHTML($body); return $newHTML; $newHTML = $dom->saveHTML(); $newHTML = str_ireplace(array('<html>', '</html>', '<body><p>', '</p></body>'), '', $newHTML); return '<p>'.$newHTML.'</p>'; } return true; } } return false; } function checkTagAndParent($tag, $forbiden){ if(!in_array($tag->tagName, $forbiden)){ if($tag->parentNode){ $parentTags = array_filter(explode("/", preg_replace("/\[[^]]+\]/", "", $tag->getNodePath()))); foreach($parentTags as $parentTag){ if(in_array($parentTag, $forbiden)){ return false; } } } return true; } return false; } function getNodeInnerHTML(DOMNode $element){ $innerHTML = ""; $children = $element->childNodes; foreach($children as $child){ $innerHTML .= $element->ownerDocument->saveHTML($child); } return $innerHTML; } function getNodeOuterHTML($e){ $doc = new DOMDocument(); $doc->appendChild($doc->importNode($e, true)); return $doc->saveHTML(); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0er1c
function name:  (null)
number of ops:  8
compiled vars:  !0 = $desc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, '%3Cp%3E%0A%3Cstrong%3ENappe+bouton+power%3C%2Fstrong%3E%3Cbr%3E%0A%3Cu+class%3D%22uuu%22%3E%3Ci+title%3D%22a+bouton+power+b%22%3ENappe+bouton+power%2C+compatible+Samsung+Galaxy+S6+Edge+G925F.%3C%2Fi%3E%3C%2Fu%3E%0A%3C%2Fp%3E'
    7     1        INIT_FCALL_BY_NAME                                       'canLinkThisWordIn'
          2        SEND_VAL_EX                                              'bouton+power'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAL_EX                                              <true>
          5        DO_FCALL                                      0  $2      
          6        ECHO                                                     $2
   73     7      > RETURN                                                   1

Function canlinkthiswordin:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 34, Position 2 = 108
Branch analysis from position: 34
2 jumps found. (Code = 78) Position 1 = 35, Position 2 = 108
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 107
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 105
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 105
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 107
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 108
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 108
filename:       /in/0er1c
function name:  canLinkThisWordIn
number of ops:  111
compiled vars:  !0 = $word, !1 = $html, !2 = $linkIt, !3 = $forbidenTagsArray, !4 = $dom, !5 = $xpath, !6 = $wordTags, !7 = $wordTag, !8 = $sensitiveWord, !9 = $newWordTagLink, !10 = $newWordTagHtml, !11 = $helper, !12 = $newWordTag, !13 = $body, !14 = $newHTML
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   12     3        ASSIGN                                                   !3, <array>
   13     4        NEW                                              $16     'DOMDocument'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !4, $16
          7        INIT_METHOD_CALL                                         !4, 'loadHTML'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0          
         10        NEW                                              $20     'DOMXpath'
         11        SEND_VAR_EX                                              !4
         12        DO_FCALL                                      0          
         13        ASSIGN                                                   !5, $20
   14    14        INIT_METHOD_CALL                                         !5, 'query'
         15        INIT_FCALL                                               'strtoupper'
         16        SEND_VAR                                                 !0
         17        DO_ICALL                                         $23     
         18        CONCAT                                           ~24     '%2F%2F%2A%5Btext%28%29%5Bcontains%28translate%28.%2C+%27', $23
         19        CONCAT                                           ~25     ~24, '%27%2C+%27'
         20        INIT_FCALL                                               'strtolower'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $26     
         23        CONCAT                                           ~27     ~25, $26
         24        CONCAT                                           ~28     ~27, '%27%29%2C+%27'
         25        INIT_FCALL                                               'strtolower'
         26        SEND_VAR                                                 !0
         27        DO_ICALL                                         $29     
         28        CONCAT                                           ~30     ~28, $29
         29        CONCAT                                           ~31     ~30, '%27%29%5D%5D'
         30        SEND_VAL_EX                                              ~31
         31        DO_FCALL                                      0  $32     
         32        ASSIGN                                                   !6, $32
   16    33      > FE_RESET_R                                       $34     !6, ->108
         34    > > FE_FETCH_R                                               $34, !7, ->108
   17    35    >   INIT_FCALL_BY_NAME                                       'checkTagAndParent'
         36        SEND_VAR_EX                                              !7
         37        SEND_VAR_EX                                              !3
         38        DO_FCALL                                      0  $35     
         39      > JMPZ                                                     $35, ->107
   18    40    > > JMPZ                                                     !2, ->105
   25    41    >   ASSIGN                                                   !8, !0
   26    42        CONCAT                                           ~37     '%3Ca+href%3D%22%23%22+title%3D%22%23%22+class%3D%22internalink%22%3E%3Cb%3E', !8
         43        CONCAT                                           ~38     ~37, '%3C%2Fb%3E%3C%2Fa%3E'
         44        ASSIGN                                                   !9, ~38
   27    45        INIT_FCALL                                               'str_ireplace'
         46        SEND_VAR                                                 !8
         47        SEND_VAR                                                 !9
         48        INIT_FCALL_BY_NAME                                       'getNodeInnerHTML'
         49        SEND_VAR_EX                                              !7
         50        DO_FCALL                                      0  $40     
         51        SEND_VAR                                                 $40
         52        DO_ICALL                                         $41     
         53        ASSIGN                                                   !10, $41
   28    54        NEW                                              $43     'DOMDocument'
         55        DO_FCALL                                      0          
         56        ASSIGN                                                   !11, $43
         57        INIT_METHOD_CALL                                         !11, 'loadHTML'
         58        SEND_VAR_EX                                              !10
         59        DO_FCALL                                      0          
   29    60        INIT_METHOD_CALL                                         !4, 'createElement'
         61        SEND_VAL_EX                                              'b'
         62        DO_FCALL                                      0  $47     
         63        ASSIGN                                                   !12, $47
   30    64        INIT_METHOD_CALL                                         !12, 'appendChild'
         65        INIT_METHOD_CALL                                         !4, 'importNode'
         66        CHECK_FUNC_ARG                                           
         67        FETCH_OBJ_FUNC_ARG                               $49     !11, 'documentElement'
         68        SEND_FUNC_ARG                                            $49
         69        SEND_VAL_EX                                              <true>
         70        DO_FCALL                                      0  $50     
         71        SEND_VAR_NO_REF_EX                                       $50
         72        DO_FCALL                                      0          
   33    73        FETCH_OBJ_R                                      ~52     !7, 'parentNode'
         74        INIT_METHOD_CALL                                         ~52, 'replaceChild'
         75        SEND_VAR_EX                                              !12
         76        SEND_VAR_EX                                              !7
         77        DO_FCALL                                      0          
   34    78        INIT_METHOD_CALL                                         !4, 'getElementsByTagName'
         79        SEND_VAL_EX                                              'body'
         80        DO_FCALL                                      0  $54     
         81        ASSIGN                                                   !13, $54
         82        INIT_METHOD_CALL                                         !13, 'item'
         83        SEND_VAL_EX                                              0
         84        DO_FCALL                                      0  $56     
         85        ASSIGN                                                   !13, $56
   35    86        INIT_METHOD_CALL                                         !4, 'saveHTML'
         87        SEND_VAR_EX                                              !13
         88        DO_FCALL                                      0  $58     
         89        ASSIGN                                                   !14, $58
   36    90        FE_FREE                                                  $34
         91      > RETURN                                                   !14
   38    92*       INIT_METHOD_CALL                                         !4, 'saveHTML'
         93*       DO_FCALL                                      0  $60     
         94*       ASSIGN                                                   !14, $60
   39    95*       INIT_FCALL                                               'str_ireplace'
         96*       SEND_VAL                                                 <array>
         97*       SEND_VAL                                                 ''
         98*       SEND_VAR                                                 !14
         99*       DO_ICALL                                         $62     
        100*       ASSIGN                                                   !14, $62
   40   101*       CONCAT                                           ~64     '%3Cp%3E', !14
        102*       CONCAT                                           ~65     ~64, '%3C%2Fp%3E'
        103*       FE_FREE                                                  $34
        104*       RETURN                                                   ~65
   42   105    >   FE_FREE                                                  $34
        106      > RETURN                                                   <true>
   16   107    > > JMP                                                      ->34
        108    >   FE_FREE                                                  $34
   45   109      > RETURN                                                   <false>
   46   110*     > RETURN                                                   null

End of function canlinkthiswordin

Function checktagandparent:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 38
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 37
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 36
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 36
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 35
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
Branch analysis from position: 37
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0er1c
function name:  checkTagAndParent
number of ops:  40
compiled vars:  !0 = $tag, !1 = $forbiden, !2 = $parentTags, !3 = $parentTag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   49     2        INIT_FCALL                                               'in_array'
          3        FETCH_OBJ_R                                      ~4      !0, 'tagName'
          4        SEND_VAL                                                 ~4
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $5      
          7        BOOL_NOT                                         ~6      $5
          8      > JMPZ                                                     ~6, ->38
   50     9    >   FETCH_OBJ_R                                      ~7      !0, 'parentNode'
         10      > JMPZ                                                     ~7, ->37
   51    11    >   INIT_FCALL                                               'array_filter'
         12        INIT_FCALL                                               'explode'
         13        SEND_VAL                                                 '%2F'
         14        INIT_FCALL                                               'preg_replace'
         15        SEND_VAL                                                 '%2F%5C%5B%5B%5E%5D%5D%2B%5C%5D%2F'
         16        SEND_VAL                                                 ''
         17        INIT_METHOD_CALL                                         !0, 'getNodePath'
         18        DO_FCALL                                      0  $8      
         19        SEND_VAR                                                 $8
         20        DO_ICALL                                         $9      
         21        SEND_VAR                                                 $9
         22        DO_ICALL                                         $10     
         23        SEND_VAR                                                 $10
         24        DO_ICALL                                         $11     
         25        ASSIGN                                                   !2, $11
   52    26      > FE_RESET_R                                       $13     !2, ->36
         27    > > FE_FETCH_R                                               $13, !3, ->36
   53    28    >   INIT_FCALL                                               'in_array'
         29        SEND_VAR                                                 !3
         30        SEND_VAR                                                 !1
         31        DO_ICALL                                         $14     
         32      > JMPZ                                                     $14, ->35
   54    33    >   FE_FREE                                                  $13
         34      > RETURN                                                   <false>
   52    35    > > JMP                                                      ->27
         36    >   FE_FREE                                                  $13
   58    37    > > RETURN                                                   <true>
   60    38    > > RETURN                                                   <false>
   61    39*     > RETURN                                                   null

End of function checktagandparent

Function getnodeinnerhtml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/0er1c
function name:  getNodeInnerHTML
number of ops:  15
compiled vars:  !0 = $element, !1 = $innerHTML, !2 = $children, !3 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
   63     1        ASSIGN                                                   !1, ''
          2        FETCH_OBJ_R                                      ~5      !0, 'childNodes'
          3        ASSIGN                                                   !2, ~5
   64     4      > FE_RESET_R                                       $7      !2, ->12
          5    > > FE_FETCH_R                                               $7, !3, ->12
   65     6    >   FETCH_OBJ_R                                      ~8      !0, 'ownerDocument'
          7        INIT_METHOD_CALL                                         ~8, 'saveHTML'
          8        SEND_VAR_EX                                              !3
          9        DO_FCALL                                      0  $9      
         10        ASSIGN_OP                                     8          !1, $9
   64    11      > JMP                                                      ->5
         12    >   FE_FREE                                                  $7
   67    13      > RETURN                                                   !1
   68    14*     > RETURN                                                   null

End of function getnodeinnerhtml

Function getnodeouterhtml:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0er1c
function name:  getNodeOuterHTML
number of ops:  15
compiled vars:  !0 = $e, !1 = $doc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   RECV                                             !0      
   70     1        NEW                                              $2      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $2
   71     4        INIT_METHOD_CALL                                         !1, 'appendChild'
          5        INIT_METHOD_CALL                                         !1, 'importNode'
          6        SEND_VAR_EX                                              !0
          7        SEND_VAL_EX                                              <true>
          8        DO_FCALL                                      0  $5      
          9        SEND_VAR_NO_REF_EX                                       $5
         10        DO_FCALL                                      0          
   72    11        INIT_METHOD_CALL                                         !1, 'saveHTML'
         12        DO_FCALL                                      0  $7      
         13      > RETURN                                                   $7
   73    14*     > RETURN                                                   null

End of function getnodeouterhtml

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.07 ms | 1417 KiB | 27 Q