Jump to content

Script detecting changes in string or array


sidenius

Recommended Posts

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 :)

Link to comment
Share on other sites

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 />";
    }
}

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.