มอดูล:string/php trim

จาก วิกิพจนานุกรม พจนานุกรมเสรี

Lua equivalent of PHP's trim($string), which trims "\0", "\t", "\n", "\v", "\r" and " ". This is useful when dealing with template parameters, since the native parser trims them like this.

Note: this module has been written to be as fast as possible, and is considerably faster than any equivalent string patterns.


local find = string.find
local match = string.match

-- Note: PHP does not trim \f but does trim \0.
return function(text)
	local n = find(text, "[^%z\t-\v\r ]")
	return n and match(text, ".*[^%z\t-\v\r ]", n) or ""
end