> <?php > > $str1 = <<<NEWTXT > Once there was a Bar > It was red > It had some Foo > Underneath its Foobar > NEWTXT; > > $str2 = <<<OLDTXT > Once there was a Bar > It had some Foo > Underneath its Foobar > OLDTXT; > > $arr1 = explode('\n', $str1); > $arr2 = explode('\n', $str2); > > $count = 0; > > foreach ($arr1 as $line_num => $line) { > if (isset($arr2[$count] && ($line === $arr2[$count]))) { > echo "Unchanged \"" . $line . "\"\n"; > } elseif ($line !== $arr2[$count]) { > echo "Changed to \"" . $line . "\" from \"" . $arr2[$count] > . "\"\n"; > } elseif (!isset($arr2[$count])) { > echo "Added \"" . $line . "\", was " . $arr2[$count] . > "\"\n"; > } > > $count++; > } > > ?> > > Then i'd get something like this: > > Unchanged "Once there was a Bar" > Changed to "It was red" from "It had some Foo" > Changed to "It had some Foo" from "Underneath its Foobar" > Added "Underneath its Foobar" > > Where the second, third and fourth line is wrong - i added a line > between "There was a Bar" and "It had some Foo"... > > How should i do this?
Add something that goes to the next line in one file and stays on the same line in the other whenver an addition is found - and use http://nl2.php.net/manual/en/function.levenshtein.php or http://nl2.php.net/manual/en/function.similar-text.php instead of != to see if a line has changed. I've never used it myself, but the return value gives you a number indicating how "different" the strings are. If they're very different AND the number of lines is not equal you know for almost sure it's an addition. Have fun finding the difference! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php