PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
function computeDiff($from, $to) { $diffValues = array(); $diffMask = array(); $dm = array(); $n1 = count($from); $n2 = count($to); for ($j = -1; $j < $n2; $j++) $dm[-1][$j] = 0; for ($i = -1; $i < $n1; $i++) $dm[$i][-1] = 0; for ($i = 0; $i < $n1; $i++) { for ($j = 0; $j < $n2; $j++) { if ($from[$i] == $to[$j]) { $ad = $dm[$i - 1][$j - 1]; $dm[$i][$j] = $ad + 1; } else { $a1 = $dm[$i - 1][$j]; $a2 = $dm[$i][$j - 1]; $dm[$i][$j] = max($a1, $a2); } } } $i = $n1 - 1; $j = $n2 - 1; while (($i > -1) || ($j > -1)) { if ($j > -1) { if ($dm[$i][$j - 1] == $dm[$i][$j]) { $diffValues[] = $to[$j]; $diffMask[] = 1; $j--; continue; } } if ($i > -1) { if ($dm[$i - 1][$j] == $dm[$i][$j]) { $diffValues[] = $from[$i]; $diffMask[] = -1; $i--; continue; } } { $diffValues[] = $from[$i]; $diffMask[] = 0; $i--; $j--; } } $diffValues = array_reverse($diffValues); $diffMask = array_reverse($diffMask); return array('values' => $diffValues, 'mask' => $diffMask); } function diffline($line1, $line2) { $diff = computeDiff(str_split($line1), str_split($line2)); $diffval = $diff['values']; $diffmask = $diff['mask']; $n = count($diffval); $pmc = 0; $result = ''; for ($i = 0; $i < $n; $i++) { $mc = $diffmask[$i]; if ($mc != $pmc) { switch ($pmc) { case -1: $result .= '</del>'; break; case 1: $result .= '</ins>'; break; } switch ($mc) { case -1: $result .= '<del>'; break; case 1: $result .= '<ins>'; break; } } $result .= $diffval[$i]; $pmc = $mc; } switch ($pmc) { case -1: $result .= '</del>'; break; case 1: $result .= '</ins>'; break; } return $result; } |
1 |
<span class="pln">echo diffline</span><span class="pun">(</span><span class="str">'StackOverflow'</span><span class="pun">,</span> <span class="str">'ServerFault'</span><span class="pun">)</span> |
1 |
<span class="pln">S</span><span class="str"><del></span><span class="pln">tackO</span><span class="pun"><</span><span class="str">/del><ins>er</</span><span class="pln">ins</span><span class="pun">></span><span class="pln">ver</span><span class="str"><del></span><span class="pln">f</span><span class="pun"><</span><span class="str">/del><ins>Fau</</span><span class="pln">ins</span><span class="pun">></span><span class="pln">l</span><span class="str"><del></span><span class="pln">ow</span><span class="pun"><</span><span class="str">/del><ins>t</</span><span class="pln">ins</span><span class="pun">></span> |
- https://stackoverflow.com/questions/321294/highlight-the-difference-between-two-strings-in-php#
Recent Posts
Recent Comments
- LogixTree Networks on Localhost showing “Index/of” instead of displaying the Website
- AMANUR RAHMAN on Localhost showing “Index/of” instead of displaying the Website
- Christy on Support Purchase Notes in Order Emails WooCommerce
- LogixTree Networks on Localhost showing “Index/of” instead of displaying the Website
- Sofia on Localhost showing “Index/of” instead of displaying the Website