1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Compare 2 html files and display difference

Discussion in 'PHP' started by ahdsan, Jul 9, 2013.

  1. #1
    I am using a script HtmlDiff.php to compare 2 files and display and highlight differences.
    Now I want the % (percentage) of difference between them. I am attaching the HtmlDiff.php here.
    I tried to use similar_text , but it hangs there because of the large text.
     

    Attached Files:

    Solved! View solution.
    ahdsan, Jul 9, 2013 IP
  2. EmmanuelFlossie

    EmmanuelFlossie Active Member

    Messages:
    159
    Likes Received:
    11
    Best Answers:
    2
    Trophy Points:
    65
    #2
    can't you do something like the following
    
    <?php
    $count1= mb_strlen( $text1 );
    $count2= mb_strlen( $text2 );
     
    if ($text1 >= $text2){
       $count[0]['text']     = $text1;
       $count[1]['text']     = $text2;
       }else{
           $count[0]['text']     = $text2;
           $count[1]['text']     = $text1;
           }
     
    $percent = ($count[0]['text'] / $count[1]['text']) * 100;
    ?>
    
    Code (markup):
     
    EmmanuelFlossie, Jul 9, 2013 IP
    ryan_uk likes this.
  3. #3
    Not sure if this will be any better than similar_text(), but it's easy to do. Though not as accurate (well, different).

    
    function percentage($string, $string2){
       $percent = levenshtein($string, $string2);
       $l = strlen($string);
       $l2 = strlen($string2);
       
       $l = ($l2 >= $l) ? $l2 : $l; 
       
       return (1 - ($percent/$l))*100 . '%';
    }
    
    PHP:
    Takes the difference in characters between the two strings using levenshtein, then divides it by the length of the longest string, so the results will be different to similar text but makes sense mathematically. I think.
     
    blueparukia, Jul 9, 2013 IP
    ryan_uk likes this.