php - How to detect changed part of multiple texts -
i got multiple string in array should find part (=words) changed (what changed text , located). algorithm should use?
input
$strings = [ 'a' => 'blah blah. value of 123456 , 50%.', 'b' => 'blah blah. value of 10203 , 75%.', 'c' => 'blah blah. value of 9999 , 500%.', // more rows ];
output
$output = 'blah blah. value of [a=123456|b=10203|c=9999] , [a=50%|b=75%|c=500%].';
(yes, i'm going add fancy html mouseover @ point ..)
currently i'm doing experiments php-finediff, it's huge mess if want compare more 2 strings each-other. (should write huge loop check characters 1 @ time or try regular expressions or .. ?)
this should bit closer finding answer; can split each sentence arrays of words, run array_diff()
on them collectively. combine words , create arrays found match earlier:
$strings = [ 'a' => 'blah blah. value of 123456 , 50%.', 'b' => 'blah blah. value of 10203 , 75%.', 'c' => 'blah blah. value of 9999 , 500%.', ]; // turn sentences arrays of "words" (adjust necessary) $tmp = array_map(function($arr) { return explode(' ', $arr); }, $strings); // find collective differences $diff = call_user_func_array('array_diff', $tmp); // build final result $result = []; foreach ($tmp $id => $words) { foreach ($words $index => $word) { if (isset($diff[$index])) { $result[$index][$id] = $word; } else { $result[$index] = $word; } } } print_r($result);
output
array ( [0] => blah [1] => blah. [2] => value [3] => of [4] => [5] => [6] => array ( [a] => 123456 [b] => 10203 [c] => 9999 ) [7] => , [8] => [9] => [10] => [11] => array ( [a] => 50%. [b] => 75%. [c] => 500%. ) )
Comments
Post a Comment