WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
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 Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
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
Submit Site
Forex Trading Online forex trading platform

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 : 6558
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 
 

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
Grab images from one or more URLs and save them to a specified local directory.
Categories : PHP, Filesystem, Strings, Arrays
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
Display list of files within current and subdirectories (recursively) showing each file as an anchored link and each directory as a category header.
Categories : Filesystem, Directories, Arrays, PHP
XML To Array
Categories : PHP, PHP Classes, XML, Arrays
Array Insertion
Categories : PHP, PHP Classes, Arrays
PHP5 URL Object
Categories : PHP, PHP Classes, URLs, Strings
Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP
columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP
Filling an array with files from any given directory. This example is for the current PHP script's directory.
Categories : PHP, Arrays, Filesystem
3 lines of Code to extract Tar, Zip, Gzip etc..
Categories : PHP, Filesystem, PHP Classes, Compression
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
Read a file with strings and create a new file with the first half of each string
Categories : PHP, Strings, Filesystem
HTML_Graphs uses PHP to provide a consistent interface for creating HTML based charts. The user of the class sets up arrays that are passed to html_graph() which then takes care of all the messy HTML layout.
Categories : Graphics, Arrays, PHP, PHP Classes, Charts and Graphs