Jump to content

How do I highlight differences between strings?


shadowcaster

Recommended Posts

Hello. I'm not sure if this is a newbie question but imagine I have two strings:
[code]
$string1 = "The quick brown brown fox jumps over the lazy dog.";
$string2 = "The quick brown albino fox jumps over the groovy dog.";
[/code]

How would I compare the strings and then highlight the differences compared to $string1 so that the words albino and groovy stand out?
Link to comment
Share on other sites

Is this what you were after?

[code]<? $string1 = "The quick brown brown fox jumps over the lazy dog.";
    $string2 = "The quick brown albino fox jumps over the groovy dog.";
    
    $string1 = explode(" ", $string1);
    $string2 = explode(" ", $string2);
    
    $i = 0;
    foreach($string2 as $value){
        if($value != $string1[$i]){
            echo "<b>$value </b>";
        }
        else {
            echo "$value ";
        }
        $i++;
    } ?>[/code]
Link to comment
Share on other sites

One minor change to the above solution will alliviate the space the end of the new string... This procedure assumes the strings are of equal length.

[code]<?php
$string1 = "The quick brown brown fox jumps over the lazy dog.";
    $string2 = "The quick brown albino fox jumps over the groovy dog.";
    
    $string1 = explode(" ", $string1);
    $string2 = explode(" ", $string2);
    $tmp = array();
    
     foreach($string2 as $k=>$value){
        if($value != $string1[$k]){
            $tmp[] = "<b>$value </b>";
        }
        else {
            $tmp[] = $value;
        }       $i++;
    }
    echo implode(' ',$tmp);
?>[/code]

Ken
Link to comment
Share on other sites

This version will show [b]added [/b]and [missing] words.

[code]
    $string1 = "The quick brown fox jumps over the lazy dog.";
    $string2 = "The quick brown albino fox jumps the groovy dog.";

    $string1 = explode(" ", $string1);
    $string2 = explode(" ", $string2);

    $diff = array_intersect($string2, $string1);

    $tmp = array();
    foreach ($string2 as $k => $w) {
             if ($diff[$k]==$w) {
                 $tmp[$k] = $w;
             }
             else {
                   $tmp[$k] = "<b>$w</b>";
             }
    }
    $diff = array_diff($string1, $tmp);

    foreach ($diff as $k => $w) {
             $tmp[$k] .= " [<strike>$w</strike>]";
    }

    echo join (' ', $tmp);[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.