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
    Code (Text):
    1.  
    2. <?php
    3. $count1= mb_strlen( $text1 );
    4. $count2= mb_strlen( $text2 );
    5.  
    6. if ($text1 >= $text2){
    7.    $count[0]['text']     = $text1;
    8.    $count[1]['text']     = $text2;
    9.    }else{
    10.        $count[0]['text']     = $text2;
    11.        $count[1]['text']     = $text1;
    12.        }
    13.  
    14. $percent = ($count[0]['text'] / $count[1]['text']) * 100;
    15. ?>
    16.  
    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).

    PHP:
    1.  
    2. function percentage($string, $string2){
    3.    $percent = levenshtein($string, $string2);
    4.    $l = strlen($string);
    5.    $l2 = strlen($string2);
    6.    
    7.    $l = ($l2 >= $l) ? $l2 : $l;
    8.    
    9.    return (1 - ($percent/$l))*100 . '%';
    10. }
    11.  
    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.