WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Compare two texts and display a block of text with the differences between them.
Categories : PHP, PHP Classes, Filesystem, Strings, Arrays Click here to Update Your Picture
lalith nayak
Date : Jan 28th 2006
Grade : 3 of 5 (graded 5 times)
Viewed : 23554
File : No file for this code example.
Images : No Images for this code example.
Search : More code by lalith nayak
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

This text utility will allow user to pass two slightly changed texts and will display block of text with the differences in this text
Type: class, text function
Version: 0.1
Requires: PHP 4.2.2
Author: the mighty Indian

[code]
<?
/*
    *---------------------------------------------------------------------------------------------------
    * UTILITY DESCRIPTION
    *---------------------------------------------------------------------------------------------------
    *    File Name:          ContentDiff.class.php
    *    Class Name:         ContentDiff()
    *    Class Description:  This text utility will allow user to pass two slightly changed texts and 
    *                        will display block of text with the differences in this text.
    *                        For example: 
    *                            OLD TEXT:        This is a OLD text.
    *                                            Check the result.
    *
    *                            NEW TEXT:        This is a NEW text.
    *                                            Check the result.
    *         
    *                            OUTPUT:            This is a OLD text.       -- by default text will be red color 
    *                                                                         with the line through it
    *
    *                                            This is a NEW text.          -- by default text will be blue color
    *
    *                                            Check the result.          -- text will be regular since it's unchaged
    *                                                 
    *    Version:            0.1
    *    BY:                 Val Vinder, Nick Ferguson
    *    Date:               07.01.2004
    *     Email:              valera_la@hotmail.com
    *
    *---------------------------------------------------------------------------------------------------
    * UTILITY LIMITATION
    *---------------------------------------------------------------------------------------------------
    * ! class only checks difference in lines and not separate words. May bee in the next version :)
    *
    *---------------------------------------------------------------------------------------------------
    * UTILITY LICENSE
    *---------------------------------------------------------------------------------------------------
    * GPL license. Feel Free to use it. If you will enchance the script share it.
    *---------------------------------------------------------------------------------------------------
    * CLASS VARIABLES
    *---------------------------------------------------------------------------------------------------
    *     $fileOld="temp/old_";            --------- location of the temparary stored old files ( i used temp dir)
    *     $fileNew="temp/new_";            --------- location of the temparary stored new files ( i used temp dir)
    *     $handleOld;                        --------- file handle for Old Files
    *     $handleNew;                        --------- file handle for New Files
    *
    *     $styleOld=' style="text-decoration: line-through;color:red;"  '; 
    *                                    ----------for convinience default decoration is assigned
    *                                          it's used for displaying changed old content
    *
    *     $styleNew=' style="color:blue;" ';   
    *                                     ----------for convinience default decoration is assigned
    *                                          it's used for displaying changed old content
    *
    *     $oldContentTEXT;                  ----------place holder for oldContent
    *     $newContentTEXT;                   ----------place holder for newContent
    *     $newText="";                    ----------place holder for the output
    *
    **---------------------------------------------------------------------------------------------------
    * CLASS METHODS
    *---------------------------------------------------------------------------------------------------
    *     function contentDiff($oldContent, $newContent, $styleOld="",$styleNew="")  --constructor
    *    function showDifference()    -- main method, that perform actual comparison
    *    function isOldContent()        -- perform check for an old Content, check if it's empty
    *    function writeContentToFile($oldContent,$newContent)      -- write content to the files and stores
    *                                                                 those files in the assigned temp dir
    *    function mergeTwoFiles()    -- perform merge of the two files by using unix diff command with some 
    *                                    options in order to display color difference
    *    function convertToText($lineArray)   -- converts file lines array to the text delimited by n
    *    function deleteFiles()                 -- method that deletes two temp files
    *---------------------------------------------------------------------------------------------------
    * CLASS CONSTRUCTOR
    *---------------------------------------------------------------------------------------------------
    * 
    *    Perfomed action:
    *
    *     1.set up file names
    *    2.create file handels
    *    3.check styles for displaying the text
    *    4.setting up old and new content
    *
    *     Passed parameters:-------
    *
    *     $oldContent                     ----------place holder for an old text
    *    $newContent                     ----------place holder for a new text
    *     $styleOld                       ----------place holder for the old style (i.e. css class name, class="old")
    *     $styleNew                          ----------place holder for the new style (i.e. css class name, class="new")
    *-------------------------------------------------------------------------------------------------------------
    * CLASS USAGE
    *-------------------------------------------------------------------------------------------------------------
    * 
    *
    *-------------------------------------------------------------------------------------------------------------
*/

class ContentDiff
{
     
    var   
$fileOld="temp/";
    var
$fileNew="temp/";
    var
$handleOld;
    var
$handleNew;
    var
$styleOld=' style="text-decoration: line-through;color:red;"  ';   //for convinience default decoration is assigned
   
var $styleNew=' style="color:blue;" ';   //for convinience default decoration is assigned
   
var $oldContentTEXT;       //place holder for oldContent
   
var $newContentTEXT;       //place holder for newContent
   
var $newText="";
     
   
/*
    * Constructor will accept four parameters
    * $oldContent -------------  place holder for an old text
    * $newContent -------------  place holder for a new text
    * $styleOld   -------------  place holder for the old style (i.e. css class name, class="oold")
    * $styleNew   -------------  place holder for the new style (i.e. css class name, class="new")
    */
     
   
function ContentDiff($oldContent, $newContent, $styleOld="",$styleNew="")
    {
       
$this->fileOld=$this->fileOld."old_".rand() ;
       
$this->fileNew=$this->fileNew."new_".rand();
       
$this->handleOld = fopen($this->fileOld,'w');
       
$this->handleNew = fopen($this->fileNew,'w');         
        if (!empty(
$styleOld))
           
$this->styleOld = $styleOld;
        if (!empty(
$styleNew))
           
$this->styleNew = $styleNew;
             
       
$this->oldContentTEXT = $oldContent;
       
$this->newContentTEXT = $newContent;
         
         
    }
     
    function
showDifference()
    {
       
$success = false;
        if (!
$this->isOldContent())
            return
true;
                 
        if (
$this->oldContentTEXT==$this->newContentTEXT)
        {
           
$this->newText = $this->newContentTEXT;
            return
true;
        }
         
        if (
$this->writeContentToFile($this->oldContentTEXT,$this->newContentTEXT))
           
$success = true;
             
       
$mergedLines = $this->mergeTwoFiles();
       
$this->deleteFiles();
        if(
$this->convertToText($mergedLines))
           
$success = true;
        return
$success;         
    }
     
    function
isOldContent()
    {             
        if (!empty(
$this->oldContentTEXT))
            return
true;
        else
            return
false;                 
    }
     
     
    function
writeContentToFile($oldContent,$newContent)
    {
       
$success = true;
        if (
fwrite($this->handleOld, $oldContent ) === FALSE) {
           
$this->messages[] = array("error"=>true,"message"=>"Cannot write old content to the file for merging",);                                 
           
$success = false;
        }
 
        if (
fwrite($this->handleNew, $newContent ) === FALSE) {
           
$this->messages[] = array("error"=>true,"message"=>"Cannot write new content to file for merging",);     
           
$success = false;
        }
         
       
fclose($this->handleOld);
       
fclose($this->handleNew);
         
        return
$success;
    }
     
    function
mergeTwoFiles()
    {
       
//diff options
       
$diffOpts .= " -aBbHiws --minimal --ignore-blank-lines --ignore-space-change ";
       
$diffOpts .= "--new-line-format='<font ".$this->styleNew.">%l</font>n' ";
       
$diffOpts .= "--old-line-format='<font ".$this->styleOld.">%l</font>n' "
       
exec("diff ". $diffOpts.$this->fileOld." ".$this->fileNew." 2>&1",$report,$return_val);
        return
$report;
    }
     
    function
convertToText($lineArray)
    {
       
$newText="";
        foreach(
$lineArray as $line)
           
$newText.=$line."n";
         
       
$this->newText = $newText;
        if (!empty(
$this->newText))
            return
true;
        else
            return
false;
    }
     
    function
deleteFiles()
    {
       
unlink($this->fileOld);
       
unlink($this->fileNew);
    }
}
//end of class

?> 


Example

<?
require_once("classes/ContentDiff.class.php");
           
$text1 = "This is a test";
           
$text2 = "This is a second test";             
           
$diff = new ContentDiff($text1,$text2);             
            if(
$diff->showDifference())
            {
                print
$diff->newText;
            }     
            else
            {
                print
$diff->messages;
            }   
?>



How to ifconfig down/up a list of IP's
Categories : Arrays, Strings, Filesystem, PHP
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
Grab images from one or more URLs and save them to a specified local directory.
Categories : PHP, Filesystem, Strings, Arrays
Filter - A simple class that lets you use multiple functions to create custom filters.
Categories : PHP, PHP Classes, Strings
Class that allows the PHP developer to create and manage UNIX like password files suitable for use as Apache authentication password files.
Categories : HTTP, PHP, PHP Classes, Filesystem
Bs_IniHandler is a class that can read and write ini-style files (and strings)
Categories : PHP, Filesystem, PHP Classes
This script will read all images from a folder and read the files into an array. It uses rand() to get a random number. It will display a random image from the image folder given.
Categories : PHP, Arrays, Graphics, Filesystem
XML To Array
Categories : PHP, PHP Classes, XML, Arrays
WWW interface to Unix Manual(phpMan)
Categories : Program Execution, Strings, Arrays, PHP
Tweak Array, insert/add elements to any position of your arrays - delete elements from your arrays - move elements within your arrays - replace elements from your arrays ... the array, 'dynamically' grows or shrinks to whatever we tweak it.
Categories : PHP Classes, Arrays, PHP
How to find the name of the current file?
Categories : PHP, Filesystem, Strings
Can the word DO be used in arrays?
Categories : Arrays, PHP, Strings
Working with files - return an array of files within a directory
Categories : PHP, Strings, Variables, Filesystem
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
PHP4 DirectoryIterator Class
Categories : PHP, PHP Classes, Filesystem, Directories