note 37766 added to function.array-diff
| From: | sire at coolquotescollection dot com | Date: | Tue, 25 Nov 2003 22:52:14 +0000 |
| Subject: | note 37766 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-60988@lists.php.net to get a copy of this message | ||
Continuing from r.kirschke's excellent diff function (above), here's a function that will
turn the result into nicely formatted HTML:
<?php
echo "<html><body bgcolor=white>";
$oldString = "Once there was a boy named Bart and a girl named Lisa.";
$newString = "Once upon a time there was a girl named Lisa.";
echo "Old String: " . $oldString . "<br>";
echo "New String: " . $newString . "<br>";
echo "Difference: " . diff_to_html($oldString, $newString);
// Returns a nicely formatted html string
function diff_to_html($oldString, $newString)
{
$a1 = explode(" ", $oldString);
$a2 = explode(" ", $newString);
$result = arr_diff($a1, $a2);
foreach ($result[0] as $num => $foo)
{
$source = $result[1][$num];
$element = $result[0][$num];
switch ($source)
{
case "1":
$pre = "<font color=red><s>";
$post = "</s></font>";
break;
case "2":
$pre = "<font color=green>";
$post = "</font>";
break;
case "b":
$pre = "";
$post = "";
break;
}
// VERTICAL OUTPUT:
// $return .= $num . $pre . " " . $source .
// " " . $element . $post . "<br>";
// READABLE OUTPUT:
$return .= $pre . $element . $post . " ";
}
return $return;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-diff.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37766
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37766&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37766&report=yes
Search -- http://master.php.net/manage/user-notes.php