# How do I highlight differences between strings?

4 replies to this topic

### #1 shadowcaster

shadowcaster
• Members
• Member
• 22 posts
• LocationUK

Posted 03 April 2006 - 11:54 PM

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

How would I compare the strings and then highlight the differences compared to \$string1 so that the words albino and groovy stand out?
--Lurking out of sight, Night shall fall and feed my frenzy, Beneath an eerie moon a change comes over me--[br]Check out Symphony X (The oddysey album) if you like heavy metal

### #2 sanfly

sanfly
• Members
• Advanced Member
• 344 posts
• LocationNew Zealand

Posted 04 April 2006 - 12:15 AM

Is this what you were after?

```<? \$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++;
} ?>```

If you're not part of the solution, you're part of the precipitate

### #3 kenrbnsn

kenrbnsn
• Staff Alumni
• Advanced Member
• 8,235 posts
• LocationHillsborough, NJ, USA

Posted 04 April 2006 - 12:05 PM

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.

```<?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);
?>```

Ken

### #4 shadowcaster

shadowcaster
• Members
• Member
• 22 posts
• LocationUK

Posted 05 April 2006 - 01:07 AM

Cheers guys
--Lurking out of sight, Night shall fall and feed my frenzy, Beneath an eerie moon a change comes over me--[br]Check out Symphony X (The oddysey album) if you like heavy metal

### #5 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,021 posts

Posted 05 April 2006 - 09:39 AM

This version will show added and [missing] words.

```    \$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);```

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received

 |baaGrid| easy data tables - and more|baaChart| easy line, column and pie charts

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users