function.addslashes

function.addslashes

[code=python] ¶

def addslashes(s): ¶
l = list(s) ¶
d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"} ¶
for i in xrange(len(l)): ¶
l[i] = d.get(l[i], l[i]) ¶
return ''.join(l
return ''.join(d.get(c, c) for c in s) ¶

s = "John 'Johny' Doe (a.k.a. \"Super Joe\")\\\0" ¶
print s ¶
print addslashes(s) ¶
#John 'Johny' Doe (a.k.a. "Super Joe")\ ¶
#John \'Johny\' Doe (a.k.a. \"Super Joe\")\\\ ¶
[/code]