Need Help Optimizing php string difference function

Solution:

Don’t reinvent the wheel. This is the sort of thing that is easy to get wrong and hard to get right.

Check out the Text_Diff Pear Package. I have used it for this sort of thing and it is very well done.