-1

Example, comparing these two strings:

Hello Jake, blah blah blah. Sent at 1:23 AM

Hello Ben, blah blah blah. Sent at 3:12 PM

Should produce:

Hello [variable], blah blah blah. Sent at [variable]

I don't need to show the old one compared to the new one, just remove the differences (or in this case replace them with the text "[variable]").

Farzher
  • 13,934
  • 21
  • 69
  • 100
  • 2
    Will help you to solve your own attempts for free, but will only write your code for you for money – Mark Baker Mar 05 '12 at 20:12
  • possible duplicate of [Highlight the difference between two strings in PHP](http://stackoverflow.com/questions/321294/highlight-the-difference-between-two-strings-in-php) – Jordan Running Mar 05 '12 at 20:32

2 Answers2

4

split on space (\s) into arrays.

Loop through arrays to compare, when the values dont match, replace with [variable], use implode() to make back to a string

Andrew Hall
  • 3,058
  • 21
  • 30
  • 2
    What if one says "Hello fthaluhft9awt7 7f8aw78t jaw78f87tj aw, blah blah blah. Sent at 3:12 PM"? – Farzher Mar 05 '12 at 20:09
  • You'd loop through both arrays instead of just using [`array_diff_assoc()`](http://www.php.net/manual/en/function.array-diff-assoc.php), Andrew? – Jordan Running Mar 05 '12 at 20:11
  • 1
    @Sarcsam - You can still compare arrays with different lengths using functions like array_intersect() – Mark Baker Mar 05 '12 at 20:14
  • Yes Jordan. You wouldnt need the associative version anyway. But array_diff() only returns the differences. Mark Bakers suggestion is valid if the strings, and therefore arrays differ in length – Andrew Hall Mar 05 '12 at 20:14
0
$string1 = explode(" ","Hello Jake, blah blah blah. Sent at 1:23 AM");
$string2 = explode(" ","Hello Ben, blah blah blah. Sent at  3:12 PM");
$finalString = explode(" ","Hello Ben, blah blah blah. Sent at  3:12 PM");


if($string1 > $string2)
{
$length = count($string2);
}
else
{
$length = count($string1);
}

for($i = 0; $i < $length; $i++)
{
if($string1[i] != $string2[i])
{ 
$finalString[i] = "[variable]";
}
}

$finalString = implode(" ", $finalString);
echo $finalString;
Joel
  • 587
  • 1
  • 5
  • 17