Php MySQL仅获取已更改的特定文本字符串
我有一个员工用来添加评论和其他信息的数据库。评论可能会变得很长,我想知道是否有办法只获取更改后的文本 例如:Php MySQL仅获取已更改的特定文本字符串,php,mysql,Php,Mysql,我有一个员工用来添加评论和其他信息的数据库。评论可能会变得很长,我想知道是否有办法只获取更改后的文本 例如: $before_text = "This is a long piece of text where the employee has made a comment about the STARTER of their project. Notes and information go here, blah, blah, blah..."; $after_text = "This is
$before_text = "This is a long piece of text where the employee has made a comment about the STARTER of their project. Notes and information go here, blah, blah, blah...";
$after_text = "This is a long piece of text where the employee has made a comment about the STATUS of their project. Notes and information go here, blah, blah, blah...";
当我比较这两者时,我得到了这样一个事实:文本已从$before\u text
更改为$before\u text
,但我希望以如下变量结束:
$change = "'STARTER' changed to 'STATUS'";
。。。这样我就可以把它写进日志了。其中一些评论非常长,我不得不以一个包含两个大条目的日志结束,以描述发生了什么变化
有没有一种方法可以只提取在两个文本/字符串变量之间发生变化的文本?以下是一些让您开始学习的内容。我为每个项目创建了一个数组,对数组进行差分以获得新值,然后使用新值的索引来获得新值
$before_text = "This is a long piece of text where the employee has made a comment about the STARTER of their project. Notes and information go here, blah, blah, blah...";
$after_text = "This is a long piece of text where the employee has made a comment about the STATUS of their project. Notes and information go here, blah, blah, blah...";
$arr1 = explode(' ', $before_text);
$arr2 = explode(' ', $after_text);
$diff = array_diff($arr1, $arr2);
print_r($diff);
$new = $arr2[key($diff)];
echo $new;
这将返回:
Array
(
[16] => STARTER
)
STATUS
但这里有一个警示故事:如果用户更改了多个单词或做了一些其他奇怪的事情,您将不得不进行一些循环和排序,以使其接近正确。YMMV希望您尝试显示文本前的
$和文本后的$之间的差异
<?php
$string_old = "hello this is a demo page";
$string_new = "hello this is a beta page";
$diff = get_decorated_diff($string_old, $string_new);
echo "<table>
<tr>
<td>".$diff['old']."</td>
</tr>
<tr>
<td>".$diff['new']."</td>
</tr>
</table>";
如果第一个和最后一个单词被更改了怎么办?字符串在哪里不同对我来说并不重要。我只是想说明差异,而不是整个变量的变化。如果问题只是“STARTER”和“STATUS”这两个词,那么这应该足够简单,并将其与分解字符串(可能是三元字符串)进行比较。问题是,那些是唯一的单词,在这2个字符串里面吗?另一个要考虑的是,它们可能会改变在评论中出现多次的单词的一个出现。如果不提供word在注释中的更改位置的上下文,日志就没有什么特别的帮助。你可能想重新考虑一下你在这里想要达到的目标。也许这会对你有所帮助。我的里程总是不同的。。。我来试一试,看看会发生什么。我完全希望在
之前的和
之后的之间会有多个编辑。(我知道这不会是件容易的事)我不确定我是否会鼓励开始走这条路。特别是当已经有工具可以帮你搬运重物的时候。“其他奇怪的事情”包括添加或删除任何文本等基本更改。也许是作为一个“我能做到这一点”的项目,但我不会在现实世界的工作项目中进行这场斗争。@siddhesh建议的horde\u text\u diff
项目(不再与Pear合作)似乎与我所寻找的最接近。不过,我同意你的评估——不用于生产。嗯,你突然想到了一个有趣的想法:为什么不给已经改变的单词添加颜色呢?我想那是可行的。是的,没错。我只需要知道发生了什么变化。所以这里是$start
表示更改单词前的文本&$end
表示更改单词后的文本,以便满足您的要求。。。只需抛出$new_diff
和$old_diff
$new=“$new_diff”$old=“$old_diff”;返回数组(“旧”=>$old,“新”=>$new)代码>
function get_decorated_diff($old, $new){
$from_start = strspn($old ^ $new, "\0");
$from_end = strspn(strrev($old) ^ strrev($new), "\0");
$old_end = strlen($old) - $from_end;
$new_end = strlen($new) - $from_end;
$start = substr($new, 0, $from_start);
$end = substr($new, $new_end);
$new_diff = substr($new, $from_start, $new_end - $from_start);
$old_diff = substr($old, $from_start, $old_end - $from_start);
$new = "$start<ins style='background-color:#ccffcc'>$new_diff</ins>$end";
$old = "$start<del style='background-color:#ffcccc'>$old_diff</del>$end";
return array("old"=>$old, "new"=>$new);
}