|
On Thu, Feb 21, 2013 at 11:54 PM, Kevin Martin <kev82@khn.org.uk> wrote:
From the 5.2 manual:
On 21 Feb 2013, at 12:48, Marc Balmer wrote:
> How should "%q" format it's parameter?
"The q option formats a string between double quotes, using escape sequences when necessary to ensure that it can safely be read back by the Lua interpreter."
I'm not saying it's really difficult to do, I just thought there might be some code out there already, as if I do it myself, it's the kind of thing where it's really easy to write a buggy implementation.
Kev
I've never used PHP, but looking through its manual I found the addcslashes[1] function, which escapes the specified characters with backslashes in the style of C.You could create a wrapper around it to imitate the %q option of string.format like the code below (which may or may not work).==========<?phpfunction escape_lua($str){$str = addcslashes($str, "\\\"\n\0"); // Escape backslashes, double quotes, newlines and nulls (embedded zeros)return '"' . $str . '"';}?>==========