Module:Explode

From Guild of Archivists
Revision as of 20:18, 22 August 2012 by Alahmnat (talk | contribs) (Use ustring, because hell, we're Wikimedia.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Explode/doc

-- Taken from http://lua-users.org/wiki/SplitJoin
-- For all those PHP coders out there.
-- explode(seperator, string)
return {
    explode = function (d,p)
        local t, ll
        t={}
        ll=0
        if(ustring.len(p) == 1) then return {p} end
        while true do
            l=ustring.find(p,d,ll,true) -- find the next d in the string
            if l~=nil then -- if "not not" found then..
                table.insert(t, ustring.sub(p,ll,l-1)) -- Save it in our array.
                ll=l+1 -- save just after where we found it for searching next time.
            else
                table.insert(t, ustring.sub(p,ll)) -- Save what's left in our array.
                break -- Break at end, as it should be, according to the lua manual.
            end
        end
        return t
    end
}