Making Lua Like Php

lua-users home
wiki

Here lies a few functions or code snippets that make Lua behave more like PHP.

Umm... [why would you want that]? --f

Note: Some of these PHP-style functions don't do exactly the same as PHP. In some cases this is intentional.

print_r

See PHP-like print_r function in TableSerialization.

explode

Based on [PHP explode]

Example: explode(" and ","one and two and three and four") --> {"one","two","three","four"}

Compatibility: Lua 5.0, 5.1, 5.2 and (probably) 5.3

function explode(div,str) -- credit: http://richard.warburton.it
  if (div=='') then return false end
  local pos,arr = 0,{}
  -- for each divider found
  for st,sp in function() return string.find(str,div,pos,true) end do
    table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
    pos = sp + 1 -- Jump past current divider
  end
  table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  return arr
end

implode

Based on [PHP implode]

Use table.concat:

PHP implode(join,array) is equivalent to Lua table.concat(table,join)

PHP: implode(" ",array("this","is","a","test","array")) --> "this is a test array"

Lua: table.concat({"this","is","a","test","array"}," ") --> "this is a test array"

PHP Tables

PHP arrays retain the order that key, value pairs are added. This is not true for Lua by default. But such functionality can be emulated.

function phpTable(...) -- abuse to: http://richard.warburton.it
  local newTable,keys,values={},{},{}
  newTable.pairs=function(self) -- pairs iterator
    local count=0
    return function() 
      count=count+1
      return keys[count],values[keys[count]]
    end
  end
  setmetatable(newTable,{
    __newindex=function(self,key,value)
      if not self[key] then table.insert(keys,key)
      elseif value==nil then -- Handle item delete
        local count=1
        while keys[count]~=key do count = count + 1 end
        table.remove(keys,count)
      end
      values[key]=value -- replace/create
    end,
    __index=function(self,key) return values[key] end
  })
  for x=1,table.getn(arg) do
    for k,v in pairs(arg[x]) do newTable[k]=v end
  end
  return newTable
end

Example Usage:

-- arguments optional
test = phpTable({blue="blue"},{red="r"},{green="g"})

test['life']='bling'
test['alpha']='blong'
test['zeta']='blast'
test['gamma']='blue'
test['yak']='orange'
test['zeta']=nil -- delete zeta

for k,v in test:pairs() do
	print(k,v)
end

The output:

blue    blue
red     r
green   g
life    bling
alpha   blong
gamma   blue
yak     orange

preg_replace

Based on [PHP preg_replace]

Example: preg_replace("\\((.*?)\\)","", " Obvious exits: n(closed) w(open) rift")

The following preg_replace variants support using the wildcards %n in the replace.

1. Using Lua-style regular expressions:

function preg_replace(pat,with,p)
  return (string.gsub(p,pat,with))
end

2. Using PCRE or POSIX regular expressions:

function preg_replace(pat,with,p)
  return (rex.gsub(p,pat,with))
end

Nutria

[Nutria] is a PHP Standard Library written in Lua.

serialize()

[lua-phpserialize] module implements serialization of Lua tables to PHP serialize() format.

See Also


RecentChanges · preferences
edit · history
Last edited June 20, 2018 1:49 pm GMT (diff)