Module:Explode: Difference between revisions

From Guild of Archivists
(Use ustring, because hell, we're Wikimedia.)
 
m (1 revision imported)
 

Latest revision as of 07:26, 30 October 2015

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
}