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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to