PHP 2 RB

new BookmarkLockedFalling
kokenge
Senior Member
****

kokenge Avatar

Posts: 261

Post by kokenge on Jun 5, 2009 7:48:42 GMT -5

I was trying to convert some PHP code to RunBasic using a editor.
I found it very time consuming.

You have to move the $ from the beginning to the end of each field. This tries to put it on the end.

WHILE, IF, and FOREACH statements use the { and } for begin and end and } else { for else.
This tries to set the begin and end statements for these.

PHP uses "_" to separate field names. This gives you the option to use a dot or use caps for the next character.

This converts [] to ().

Anyway I wrote a quick RB program to help in the conversion.
It is not fancy, but it takes care of the bulk of the tedious stuff. As you know some things, you simply have to look at to determine what needs to be done.

This creates the converted bas program in the public directory..


' ---------------------------------------
' PHP to RB conversion
' php2rb.bas
' ---------------------------------------
html "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR>"
html "<TD BGCOLOR=steelblue ALIGN=CENTER><FONT COLOR=#FFCC00 FACE=Arial>PHP to RB conversion</TD>"
html "</TR></TABLE>"

html "<center><TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0><TR><TD nowrap><P>Convert '_' to"
radiogroup #cd, "Dots, Caps", "Caps"
#cd horizontal(1)
html "<P></TD></TR><TR><TD align=center>"
upload "Select PHP file"; file$
html "</tr></table>"

if file$ = "" then end
html "</center>"

cd$ = #cd selection$()
cr$ = chr$(13)
lf$ = chr$(10)
tab$ = chr$(9)

sqliteconnect #mem, ":memory:"
mem$ = "CREATE TABLE php (
seqNum int,
code text,
cmt int
)"
#mem execute(mem$)
seqNum = 0

'get contents of uploaded file into string variable
open file$ for binary as #f
a$ = input$(#f, LOF(#f))
close #f

ln = len(a$)
cr = instr(a,ドルcr$)
lf = instr(a,ドルlf$)

a$ = strRep$(a,ドルlf,ドルcr$) ' lf to cr
a$ = lower$(a$)
seqNum = 0
i1 = 1
[loop]
i = instr(a,ドルcr,ドルi1)
if i > 0 then

a1$ = " ";strip$(mid$(a,ドルi1,i - i1));" "
a1$ = strRep$(a1,ドル";","")

if INSTR(a1,ドルtab$) > 0 then a1$ = strRep$(a1,ドルtab,ドル" ") ' tabs to space
if INSTR(a1,ドル" ") > 0 then a1$ = strRep$(a1,ドル" "," ") ' single space
if INSTR(a1,ドル"&&") > 0 then a1$ = strRep$(a1,ドル"&&","AND") ' single space
if INSTR(a1,ドル"echo") > 0 then a1$ = strRep$(a1,ドル"echo","HTML")
if INSTR(a1,ドル" . ") > 0 then a1$ = strRep$(a1,ドル" . "," + ")
if INSTR(a1,ドル"[") > 0 then a1$ = strRep$(a1,ドル"[","(")
if INSTR(a1,ドル"]") > 0 then a1$ = strRep$(a1,ドル"]",")")
if INSTR(a1,ドル"else") > 0 then
a1$ = strRep$(a1,ドル"{","")
a1$ = strRep$(a1,ドル"}","")
end if

if cd$ <> "Caps" then
a1$ = strRep$(a1,ドル"_",".")
else
[loop1]
j = INSTR(a1,ドル"_")
if j > 0 then
a1$ = left$(a1,ドルj - 1) + upper$(mid$(a1,ドルj + 1,1)) + mid$(a1,ドルj + 2)
goto [loop1]
end if
end if
' ------------------------------
' move $ to end of variable
' ------------------------------
j1 = 0
[loop2]
j = INSTR(a1,ドル"$", j1)
if j > 0 then
j1 = INSTR(a1,ドル" ",j)

if j1 = 0 then j1 = len(a1$)
j1a = INSTR(a1,ドル"(",j)
if j1a > 0 then j1 = MIN(j1,j1a)

j1a = INSTR(a1,ドル")",j)
if j1a > 0 then j1 = MIN(j1,j1a)

j1a = INSTR(a1,ドル",",j)
if j1a > 0 then j1 = MIN(j1,j1a)
j2 = (j1 - j) - 1

if j > 0 and j1 > 0 and j2 > 0 then
a1$ = left$(a1,ドルj - 1) + mid$(a1,ドルj+1, j2) + "$" + mid$(a1,ドルj1)
goto [loop2]
end if
end if
[cmtOn]
if cmtOn > 0 then
a1$ = "'' " + a1$
if INSTR(a1,ドル"*/") > 0 then ' check for end of comments
cmtOn = 0
'print "============ COMMENT OFF ============"
aa1$ = "''" + word$(a1,1,ドル"*/")
seqNum = seqNum + 1
cmt = 1
mem$ = "INSERT INTO php VALUES(";seqNum;",'";aa1$;"',";cmt;")"
#mem execute(mem$)
'print mem$
a1$ = word$(a1,2,ドル"*/")
else
goto [skipCmt]
end if
end if

cmt = INSTR(a1,ドル"//")

if cmtOn = 0 then cmtOn = INSTR(a1,ドル"/*") ' Turn comments on
if cmtOn > 0 then
'print "============ COMMENT ON =============";a1$
aa1$ = trim$(word$(a1,1,ドル"/*"))
if aa1$ <> "" then
seqNum = seqNum + 1
cmt = INSTR(aa1,ドル"//")
mem$ = "INSERT INTO php VALUES(";seqNum;",'";aa1$;"',";cmt;")"
#mem execute(mem$)
'print mem$

end if
a1$ = word$(a1,2,ドル"/*")
goto [cmtOn]
end if
[skipCmt]
seqNum = seqNum + 1
mem$ = "INSERT INTO php VALUES(";seqNum;",'";a1$;"',";cmt;")"
#mem execute(mem$)
'print mem$

[skip]
i1 = i + 1
goto [loop]
end if

s = 1
[nxtForeach]
mem$ = "SELECT * FROM php WHERE code LIKE '% foreach %' AND seqNum >= ";s;" ORDER BY seqNum LIMIT 1"
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code,ドル"'","''")
cmt = #row cmt()
'print using("########",seqNum);" ";code$
i = INSTR(code,ドル" foreach ")
i1 = INSTR(code,ドル"{",i)
if i1 > 0 then
code$ = left$(code,ドルi1 - 1) + mid$(code,ドルi1 + 1)
code$ = strRep$(code,ドル"foreach","FOR")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
#mem execute(mem$)
'print mem$
else
mem$ = "SELECT * FROM php WHERE code LIKE '%{%' AND seqNum > ";seqNum;" ORDER BY seqNum LIMIT 1"
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code,ドル"'","''")
cmt = #row cmt()
code$ = strRep$(code,ドル"{","")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
#mem execute(mem$)
'print mem$
end if
end if
mem$ = "SELECT * FROM php WHERE code LIKE '%}%' AND seqNum >= ";seqNum;" ORDER BY seqNum LIMIT 1"
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code,ドル"'","''")
cmt = #row cmt()
code$ = strRep$(code,ドル"}","NEXT")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
#mem execute(mem$)
'print mem$
end if

s = seqNum + 1
goto [nxtForeach]
end if


s = 1
[nxtIf]
mem$ = "SELECT * FROM php WHERE code LIKE '% if %' AND seqNum >= ";s;" ORDER BY seqNum LIMIT 1"

'print mem$

#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code,ドル"'","''")
cmt = #row cmt()
'print using("########",seqNum);" ";code$
'input x
i = INSTR(code,ドル" if ")
i1 = INSTR(code,ドル"{",i)
if i1 > 0 then
code$ = left$(code,ドルi1 - 1) + " THEN " + mid$(code,ドルi1 + 1)
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
else
mem$ = "SELECT * FROM php WHERE code LIKE '%{%' AND seqNum > ";seqNum;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code,ドル"'","''")
cmt = #row cmt()
code$ = strRep$(code,ドル"{","THEN")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
end if
end if
mem$ = "SELECT * FROM php WHERE code LIKE '%}%' AND seqNum >= ";seqNum;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code,ドル"'","''")
cmt = #row cmt()
code$ = strRep$(code,ドル"}","END IF")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
end if

s = seqNum + 1
goto [nxtIf]
end if


s = 1
[nxtWhile]
mem$ = "SELECT * FROM php WHERE code LIKE '% while %' AND seqNum >= ";s;" ORDER BY seqNum LIMIT 1"

'print mem$

#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code,ドル"'","''")
cmt = #row cmt()
'print using("########",seqNum);" ";code$
i = INSTR(code,ドル" while ")
i1 = INSTR(code,ドル"{",i)
if i1 > 0 then
code$ = left$(code,ドルi1 - 1) + mid$(code,ドルi1 + 1)
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
else
mem$ = "SELECT * FROM php WHERE code LIKE '%{%' AND seqNum > ";seqNum;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code,ドル"'","''")
cmt = #row cmt()
code$ = strRep$(code,ドル"{","")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
end if
end if
mem$ = "SELECT * FROM php WHERE code LIKE '%}%' AND seqNum >= ";seqNum;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code,ドル"'","''")
cmt = #row cmt()
code$ = strRep$(code,ドル"}","WEND")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
end if

s = seqNum + 1
goto [nxtWhile]
end if

' -------------------------------
' file out in \public dir
' -------------------------------
fileOut$ = DefaultDir$ + "\public\" + file$
print "File Out:";fileOut$
OPEN fileOut$ for OUTPUT as #fo

mem$ = "SELECT * FROM php ORDER BY seqNum"
#mem execute(mem$)
WHILE #mem hasanswer()
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
cmt = #row cmt()
if cmt > 0 then code$ = strRep$(code,ドル"//","'")
if INSTR(code,ドル"\n") > 0 then code$ = strRep$(code,ドル"\n","chr$(13)")
print using("########",seqNum);" |";code$
print #fo,code$
WEND
close #fo
wait

' --------------------------------
' string replace rep str with
' --------------------------------
FUNCTION strRep$(str,ドルrep,ドルwith$)
ln = len(rep$)
ln1 = ln - 1
i = 1
while i <= len(str$)
if mid$(str,ドルi,ln) = rep$ then
strRep$ = strRep$ + with$
i = i + ln1
else
strRep$ = strRep$ + mid$(str,ドルi,1)
end if
i = i + 1
WEND
END FUNCTION

' -----------------------------------------
' strip junk
' -----------------------------------------
FUNCTION strip$(str$)
strip$ = ""
for i = 1 to len(str$)
a$ = MID$(str,ドルi,1)
a = ASC(a$)
if a > 31 then
if a < 127 then
if a$ <> "'" then
if a$ <> """" then
strip$ = strip$ + a$
end if
end if
end if
end if
next i
END FUNCTION





Last Edit: Jun 5, 2009 7:49:41 GMT -5 by kokenge
Carl Gundel - admin
Administrator
*****

Carl Gundel - admin Avatar

Posts: 550

Post by Carl Gundel - admin on Jun 6, 2009 14:49:57 GMT -5

kokenge Avatar
Anyway I wrote a quick RB program to help in the conversion.
It is not fancy, but it takes care of the bulk of the tedious stuff. As you know some things, you simply have to look at to determine what needs to be done.

This creates the converted bas program in the public directory..

Wow, you've been busy!

-Carl
rich357
Senior Member
****

rich357 Avatar

Posts: 353

Post by rich357 on Jun 22, 2009 15:04:48 GMT -5

Oh I just whipped this little old program out in just a few minutes.
Modest he is ain't he?

You should see the program I wrote for doing my "oldies" project with Liberty Basic.

Which will eventually get revived under Run Basic.