Simple splitting function that splits strings the way expected from PHP and similar languages.
splitstr(";a;b;;d;", array[], ";")
gives:
0 = ""
1 = "a"
2 = "b"
3 = ""
4 = "d"
5 = ""
splitstr(";a;b;;d;", array[], ";")
gives:
0 = ""
1 = "a"
2 = "b"
3 = ""
4 = "d"
5 = ""
Code (glbasic) Select
// Splits strings into an array, does not skip empty fields or split-characters as first/last on a line
// Considers double split-characters as an empty field ("a;b;;d") = four fields
FUNCTION splitstr2: string$, array$[], split$
LOCAL a$, n, length, found
REDIM array$[0] // Empty the array
length = LEN(string$)
WHILE n<=length
found = INSTR(string$, split$, n) // Find next delimiter
IF found>=0
a$ = MID$(string$, n, found-n) // Find the data
DIMPUSH array$[], a$ // Add it to the field in the array
n = found+1 // Next field
ELSE
a$ = MID$(string$, n, -1) // Find the data
DIMPUSH array$[], a$ // Last field, add to the array
n=length+1
ENDIF
WEND
RETURN LEN(array$[])
ENDFUNCTION