Last active
October 15, 2015 01:45
-
-
Save python1981/5333e629a10f3a8c8526 to your computer and use it in GitHub Desktop.
Convert PHP to Python (incomplete time-saver)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Instructions: | |
# 1. Launch ipython | |
# 2. manually set filename = '/path/to/file.php' | |
# 3. Copy/Paste the below code to write transformed content to ./output.txt | |
import re | |
with open(filename,'r') as f: code = f.read() | |
code = code.replace('<?php','').replace('<?','') | |
code = code.replace('array()','[]') | |
code = code.replace('function ','def ') | |
code = code.replace('var ','self.') | |
code = code.replace('$this','self') | |
code = code.replace('self::','') #Assume static calls are now moved to the module level | |
code = code.replace('static ','') #Assume static calls are now moved to the module level | |
code = code.replace('public ','') | |
code = code.replace('//','#') | |
code = code.replace(';\n','\n').replace(';#','#') | |
code = code.replace('$','') | |
code = code.replace('.=','+=').replace(' . ',' + ') #concatenation | |
code = code.replace('&&','and') | |
code = code.replace('||','or') | |
code = re.sub("in_array\((.*),(.*)\)","\\1 in \\2",code) | |
code = code.replace('array(','[') | |
code = re.sub("[ ]+=>[ ]+",": ",code) | |
code = re.sub("if\((.*)\){","if \\1:",code) | |
code = code.replace('if ','if ') | |
code = code.replace('){','):').replace('{\n',':\n') | |
code = code.replace('}else if','elif').replace('else if','elif').replace('}elif:','elif:') | |
code = code.replace('}else{','else:').replace('else{','else:').replace('}else:','else:') | |
code = code.replace('echo','print') | |
code = re.sub("([A-Z])","_\\1",code).lower() #Convert camelcase to underscore. Run before converting True/False/None etc | |
code = re.sub("if empty\((.*)\)+","if \\1 is None",code) | |
code = re.sub("if \!isset\((.*)\)+","if \\1 is None",code) | |
code = re.sub("if \is_null\((.*)\)+","if \\1 is None",code) | |
code = code.replace('NULL','None') | |
code = code.replace('null','None') | |
code = code.replace('false','False') | |
code = code.replace('true','True') | |
code = code.replace('++','+=1') | |
code = code.replace('strlen','len') | |
code = code.replace('count','len') | |
code = code.replace('\t',' ') | |
code = code.replace('->','.') | |
code = code.replace('}','') #somewhat dangerous | |
code = code.replace('floatval(','float(') | |
code = code.replace('intval(','int(') | |
#Untested additions... | |
code = code.replace('===None',' is None') | |
code = code.replace('!==None',' is not None') | |
code = code.replace('!==None',' is not None') | |
code = code.replace('array_sum','sum') | |
code = re.sub("str_repeat\((.*),(.*)\)"," \\1 * \\2 ",code) | |
code = re.sub("implode\((.*),(.*)\)"," \\1.join(\\2) ",code) | |
code = re.sub("explode\((.*),(.*)\)"," \\2.split(\\1) ",code) | |
code = re.sub("foreach\((.*) as (.*)=>(.*)\)","for \\2,\\3 in enumerate(\\1)",code) | |
code = re.sub("foreach\((.*) as (.*)\)","for \\2 in \\1",code) | |
code = re.sub("\[\]= (.*)",".append(\\1)",code) #appending new element to array | |
code = re.sub("str_replace\((.*),(.*),(.*)\)","\\3.replace(\\1,\\2)",code) | |
#comments | |
code = re.sub("/\*+","\"\"\"",code) | |
code = re.sub(" \*+/","\"\"\"",code) | |
code = re.sub("\*+/","\"\"\"",code) | |
code = re.sub(" \* ","\"\"\"",code) | |
code = re.sub("\t \* ","\"\"\"",code) | |
with open('output.txt','w') as f: f.write(code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment