Can someone help me translate a piece of code to PHP?.
I have no experience with python. It would be great if someone could translate this code to PHP or suggest something better. I have only older version of this code in PHP, which is not so effective.
I hope someone can help.
Thanks in advance
# Simple Diff for Python v 0.1
# (C) Paul Butler 2008 <http://www.paulbutler.org/>
# May be used and distributed under the zlib/libpng license
# <http://www.opensource.org/licenses/zlib-license.php>
def diff(old, new):
"""Find the differences between two lists. Returns a list of pairs, where the first value
is in ['+','-','='] and represents an insertion, deletion, or no change for that list.
The second value of the pair is the list of elements."""
ohash = {}
for i, val in enumerate(old): # Build a hash map with elements from old as keys, and
# a list of indexes as values
ohash.setdefault(val,[]).append(i)
# Find the largest substring common to old and new
lastRow = [0] * len(old)
subStartOld = subStartNew = subLength = 0
for j, val in enumerate(new):
thisRow = [0] * len(old)
for k in ohash.setdefault(val,[]):
thisRow[k] = (k and lastRow[k - 1]) + 1
if(thisRow[k] > subLength):
subLength = thisRow[k]
subStartOld = k - subLength + 1
subStartNew = j - subLength + 1
lastRow = thisRow
if subLength == 0: # If no common substring is found, assume that an insert and
# delete has taken place...
return (old and [('-', old)] or []) + (new and [('+', new)] or [])
else: # ...otherwise, the common substring is considered to have no change, and we recurse
# on the text before and after the substring
return diff(old[:subStartOld], new[:subStartNew]) + \
[('=', new[subStartNew:subStartNew + subLength])] + \
diff(old[subStartOld + subLength:], new[subStartNew + subLength:])
original source: github.com/paulgb/simplediff
article: simple diff in python