Php 如何比较两个字符串并确定其差异?

Php 如何比较两个字符串并确定其差异?,php,string,Php,String,我有两个字符串,如下所示: $str1 = "this is a text"; $str2 = "this is a text which is edited"; 正如您所看到的,它们之间有两个不同之处:删除了之后的

我有两个字符串,如下所示:

$str1 = "this is
         a text";

$str2 = "this is a text
         which is edited";
正如您所看到的,它们之间有两个不同之处:删除了

之后的
(在
$str1
中)和添加了
,编辑了
。现在我需要检测这些差异,我该怎么做

实际上,我需要向我的网站用户展示编辑的内容。与stackoverflow编辑页面完全相同(该页面使用红色和绿色突出显示差异)

应该有用


如果加载php扩展xdiff,那么命令xdiff\u string\u diff将为您生成一个包含两个字符串之间差异的统一diff

<?
$str1 = "this is
         a text";
$str2 = "this is a text
         which is edited";

$diff = xdiff_string_diff($str1, $str2, 1);
if (is_string($diff)) {
    echo "Differences between two strings:\n";
    echo $diff;
}else{
    echo "The two strings look the same to me.";
}
?>

如果加载php扩展xdiff,那么命令xdiff\u string\u diff将为您生成一个包含两个字符串之间差异的统一diff

<?
$str1 = "this is
         a text";
$str2 = "this is a text
         which is edited";

$diff = xdiff_string_diff($str1, $str2, 1);
if (is_string($diff)) {
    echo "Differences between two strings:\n";
    echo $diff;
}else{
    echo "The two strings look the same to me.";
}
?>


str_replace()
至少需要3个参数。顺便说一句,我正在尝试创建类似“Ok”的东西。。。但是,如果你把它倒过来,它也会给你拿出来的东西吗?还是我太累了而不能思考?更改str1和str2的位置
str_replace()
至少需要3个参数。顺便说一句,我正在尝试创建类似Ok的东西。。。但是,如果你把它倒过来,它也会给你拿出来的东西吗?还是我太累了而不能思考?更改str1和str2的位置如果您使用*NIX系统,您可能会执行类似于
$diff=shell\u exec(“diff”)的操作如果您使用*NIX系统,您可能会执行类似于
$diff=shell\u exec(“diff”)的操作