Forum Discussion

ajporterfield's avatar
14 years ago

PHP explode/implode Brightscript Equivalents

PHP has many useful built-in array functions that I miss when developing Roku channels with Brightscript. So, I decided to write (and share) a couple equivalents in Brightscript. Please feel free to use these as is and/or comment on how your versions are better/more efficient than what I've posted:)


function explode(delimeter, string)
result = []
position1 = 1
position2 = Instr(position1, string, delimeter)
delimeterLen = Len(delimeter)
stringLen = Len(string)

while position2 <> 0
result.Push(Mid(string, position1, position2 - position1))
position1 = position2 + delimeterLen
position2 = Instr(position1, string, delimeter)
end while

if position1 <= stringLen
result.Push(Mid(string, position1))
else if position1 - 1 = stringLen
result.Push("")
end if

return result
end function

function implode(glue, pieces)
result = ""
for each piece in pieces
if result <> ""
result = result + glue
end if
result = result + piece
end for

return result
end function

2 Replies

  • I believe the Tokenize() method on a string will do what your explode function does.
  • "TheEndless" wrote:
    I believe the Tokenize() method on a string will do what your explode function does.


    You could also try roRegEx::Split()