PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Nov 28, 2014 1:00 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun May 27, 2012 12:38 am 
Offline
Forum Commoner
User avatar

Joined: Fri Jul 16, 2010 7:17 am
Posts: 43
Location: Apple Valley, California
Anyone have any use for something like this?
Syntax: [ Download ] [ Hide ]
<table width="100%" style="font-family: monospace;">
<tbody>
<tr>
<td>
Current File:
</td>
<td>
Old File:
</td>
</tr>
<tr>
<td valign="top">
<?php
        $curfile = explode("\n", file_get_contents('testdoc.txt'));
        $oldfile = explode("\n", file_get_contents('testdoc_old.txt'));
        $linecount = (count($curfile) > count($oldfile)) ? count($curfile) : count($oldfile);

        for ($c = '0'; $c != $linecount; $c++) {
                if ($curfile[$c] === $oldfile[$c]) {
                        echo $curfile[$c] . "<br>\n";
                } else {
                        echo '<b>' . $curfile[$c] . "</b><br>\n";
                }
        }
?>
</td>
<td valign="top">
<?php
        echo str_replace("\n", "<br>\n", file_get_contents('testdoc_old.txt'));
?>
</td>
</tr>
</tbody>
</table>

If so, enjoy!


Top
 Profile  
 
PostPosted: Sun May 27, 2012 7:18 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2775
Location: .za
Just a note; a integer is doesn't require single quotes (or any quotes) when assigning it to a variable;
Syntax: [ Download ] [ Hide ]
<?php
$c = '0'; $c != $linecount; $c++
// to this
$c = 0; $c != $linecount; $c++
?>

The script will probably still work but it's not required

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Mon May 28, 2012 12:03 am 
Offline
Forum Commoner
User avatar

Joined: Fri Jul 16, 2010 7:17 am
Posts: 43
Location: Apple Valley, California
Update - Comparison of Individual Words, bug fix.
Syntax: [ Download ] [ Hide ]
<?php

// Copyright (C) 2012 Jacob Barkdull
//
//      This program is free software: you can redistribute it and/or modify
//      it under the terms of the GNU Affero General Public License as
//      published by the Free Software Foundation, either version 3 of the
//      License, or (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU Affero General Public License for more details.
//
//      You should have received a copy of the GNU Affero General Public License
//      along with this program.  If not, see <http://www.gnu.org/licenses/>.


// Display source code
if (basename($_SERVER["PHP_SELF"]) == basename(__FILE__) && isset($_GET["source"])) {
        header("Content-type: text/plain");
        exit(file_get_contents(basename($_SERVER["PHP_SELF"])));
}

// HTML Table
echo '<table width="100%">' . "\n<tbody>\n<tr>\n";
echo '<td valign="top">' . "\nCurrent Version:<hr>\n<pre>\n";

// Replace '<' and '>' Characters
function replace($input) {
        return str_replace(array('<', '>'), array('&lt;', '&gt;'), $input);
}

// Line Arrays
$cv = explode("\n", replace(file_get_contents('testdoc.txt'))); // Current Version
$ov = explode("\n", replace(file_get_contents('testdoc_old.txt'))); // Old Version

// Count Lines - Set to Longer Version
$lc = (count($cv) > count($ov)) ? count($cv) : count($ov);

// Fix Mismatched Line Counts
for ($flc = count($ov); $flc != $lc; $flc++) {
        $ov["$flc"] = '';
}

for ($l = '0'; $l != $lc; $l++) {
        // Word Arrays
        $cw = explode(' ', $cv["$l"]); // Current Version
        $ow = explode(' ', $ov["$l"]); // Old Version

        // Count Words - Set to Longer Version
        $wc = (count($cw) > count($ow)) ? count($cw) : count($ow);

        // Fix Mismatched Word Counts
        for ($fwc = count($ow); $fwc != $wc; $fwc++) {
                $ow["$fwc"] = '';
        }

        // If each line is identical, just echo the normal line. If not,
        // check if each word is identical. If not, wrap colored "<b>"
        // tags around the mismatched words.
        if ($cv["$l"] !== $ov["$l"]) {
                for ($w = '0'; $w != $wc; $w++) {
                        if ($cw["$w"] === $ow["$w"]) {
                                echo $cw["$w"];
                                echo ($w != ($wc - 1)) ? ' ' : "\n";
                        } else {
                                echo '<b style="color: #00BB00;">' . $cw["$w"];
                                echo ($w != ($wc - 1)) ? '</b> ' : "</b>\n";
                        }
                }
        } else {
                echo $cv["$l"] . "\n";
        }
}

// Ending HTML Tags
echo "</pre>\n</td>\n<td>&nbsp;</td>\n";
echo '<td valign="top">' . "\nOld Version:<hr>\n<pre>\n";

// Read and Display Old Version
echo replace(file_get_contents('testdoc_old.txt')) . "\n";
echo "</pre>\n</td>\n</tr>\n</tbody>\n</table>";
?>
 


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group