sidenius Posted June 28, 2012 Share Posted June 28, 2012 Hello I've been struggling with the following problem for a while and I'm ob the verge of giving up! I have a textarea in which a user enters some text. Each word in the string entered can be formatted by the user. This is simple enough and the formatted string along with an undormatted string is stored in a db. However when a user whishes to edit the text at a later point in time I need to know which words in the string has been deleted or changed (in order to keep the formatting of the words not deleted or changed). So my question is whether you guys know if it is possible to compare the original string entered with the edited one - letting me know what (which words) has been changed? An example string: He was very happy. The user has formatted the words was and very. So an areay would be Arr[0] = He Arr[1] = was Arr[1][0] = formatted Arr[2] = very Arr[2][0] = formatted Arr[3] = happy Now if I cange the string to He was happy with life I need the new array to be Arr[0] = He Arr[1] = was Arr[1][0] = formatted Arr[2] = with Arr[3] = life Can this be done? Do you know of any algorithms or functions which can achieve this? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/264919-script-detecting-changes-in-string-or-array/ Share on other sites More sharing options...
btherl Posted June 28, 2012 Share Posted June 28, 2012 A good start might be here: http://en.wikipedia.org/wiki/Diff Quote Link to comment https://forums.phpfreaks.com/topic/264919-script-detecting-changes-in-string-or-array/#findComment-1357634 Share on other sites More sharing options...
btherl Posted June 28, 2012 Share Posted June 28, 2012 Diff is line oriented, but you can just as well use a word as your basic unit instead of a line, and apply the same algorithm. Quote Link to comment https://forums.phpfreaks.com/topic/264919-script-detecting-changes-in-string-or-array/#findComment-1357635 Share on other sites More sharing options...
Barand Posted June 28, 2012 Share Posted June 28, 2012 if you have already broken the text into arrays of words <?php $a = array ('He', 'was', 'happy', 'with', 'life'); $b = array ('He', 'was', 'formatted', 'with', 'life'); $c = array_diff($a, $b); if ($c) { foreach ($c as $k=>$v) { echo "Original: {$a[$k]}<br />"; echo "Change: {$b[$k]}<br /><br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264919-script-detecting-changes-in-string-or-array/#findComment-1357660 Share on other sites More sharing options...
sidenius Posted June 28, 2012 Author Share Posted June 28, 2012 Yes that would work to some extent, however if we consider the following two scenaries: He was very happy about life. (string one where happy is formatted) All of the people hated him. He was very happy about life (altered string or array, still having formatted the word happy). In this case wouldn't diff tell me that everything has changed? Herebu the formatting is lost. Or if the word happy is found more than once? Quote Link to comment https://forums.phpfreaks.com/topic/264919-script-detecting-changes-in-string-or-array/#findComment-1357669 Share on other sites More sharing options...
redarrow Posted June 28, 2012 Share Posted June 28, 2012 read this. http://uk3.php.net/manual/en/ref.array.php you asked for this from your last question, but you study the code from the link. do a trail show us your understanding php, we can program for you but you wont learn will yaa http://uk3.php.net/manual/en/function.array-unique.php Quote Link to comment https://forums.phpfreaks.com/topic/264919-script-detecting-changes-in-string-or-array/#findComment-1357740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.