3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string_old = "This is the second div that we are using to check if the original data can be seen & Yes It is Working"; $string_new = "This is the first div that we are using to check if the changed data can be seen & Yes It is Working"; function getDiffed($string_old, $string_new){ $A = explode(" ", $string_old); $B = explode(" ", $string_new); $LCS = []; for($i = 0; $i < count($A); ++$i){ for($j = 0; $j < count($B); ++$j){ if($A[ $i ] == $B[ $j ]){ $LCS[ $i ][ $j ] = max($LCS[ $i ][ $j - 1 ] ?? 0, ($LCS[ $i - 1 ][ $j - 1 ] ?? 0) + 1); }else{ $LCS[ $i ][ $j ] = max($LCS[ $i - 1 ][ $j ] ?? 0, $LCS[ $i ][ $j - 1 ] ?? 0); } } } // get the LCS string space separated. $i = count($A) - 1; $j = count($B) - 1; $commonWords = []; while($i >= 0 && $j >= 0){ if($A[ $i ] == $B[ $j ]){ $commonWords[] = $A[ $i ]; $i--; $j--; }else if(($LCS[ $i ][$j - 1] ?? 0) > ($LCS[ $i - 1 ][ $j ] ?? 0)){ $j--; }else{ $i--; } } $commonWords = array_reverse($commonWords); // add red marks if any for the old string for($i = 0, $ptr = 0; $i < count($A); ++$i){ if($ptr == count($commonWords) || $A[ $i ] !== $commonWords[ $ptr ]){ $A[ $i ] = "<del style='background-color:#ffcccc'> " .$A[ $i ] . "</del>"; }else{ $ptr++; } } // add green marks if any for the new string for($i = 0, $ptr = 0; $i < count($B); ++$i){ if($ptr == count($commonWords) || $B[ $i ] !== $commonWords[ $ptr ]){ $B[ $i ] = "<ins style='background-color:#ccffcc'> " .$B[ $i ] . "</ins>"; }else{ $ptr++; } } return ['old' => implode(' ', $A), 'new' => implode(' ', $B)]; } $diff = getDiffed($string_old, $string_new); $doc = <<<DOC <center> <h2>Old String VS New String</h2> <table border=1 cellpadding=15> <tr align=center> <td>Old</td> <td>New</td> </tr> <tr> <td>$diff[old]</td> <td>$diff[new]</td> </tr> </table> </center> DOC; echo $doc;
Output for 7.4.0, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
<center> <h2>Old String VS New String</h2> <table border=1 cellpadding=15> <tr align=center> <td>Old</td> <td>New</td> </tr> <tr> <td>This is the <del style='background-color:#ffcccc'> second</del> div that we are using to check if the <del style='background-color:#ffcccc'> original</del> data can be seen & Yes It is Working</td> <td>This is the <ins style='background-color:#ccffcc'> first</ins> div that we are using to check if the <ins style='background-color:#ccffcc'> changed</ins> data can be seen & Yes It is Working</td> </tr> </table> </center>
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
96.57 ms | 408 KiB | 5 Q