Re: Multi-User Text-Editing

From: Date: Tue, 07 Sep 2004 14:53:08 +0000
Subject: Re: Multi-User Text-Editing
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-196037@lists.php.net to get a copy of this message
Okay, i got the general idea, now it all comes down to the actual writing. I'm making a function that compares two strings (the one in the source i read used an exec() call, but i'd like to do it all in PHP) per-line. This poses some difficulties. Here's the scenario: I want to determine whether or not two lines are identical. If they're not: - Has the line simply been edited? - Is it a completely new line? - Has the line been deleted? To do so i'll have to run a loop, checking each line. But here's where the problem is: <?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? -- Daniel Schierbeck

« previous php.general (#196037) next »