I have a series of strings in the form:
{ method_name { $key1 = 'quoted value' , $key2 = __('literal value'); }}
// Missing method_name and final semi-colon
// Still valid
{{ $key1 = 'quoted value' , $key2 = __('literal value') }}
// Optional key values
{ method_name { $key1 = , $key2 = __('literal value'); }}
{ method_name { $key1, $key2 = __('literal value'); }}
// Any number of values
{ method_name { $key1 = 'quoted value' , $keyN = 3.14; }}
Currently, I use a series of preg_split
and trim
. This is part of a custom template engine where method_name
informs the parser which method to call and $key = value
will be passed to the method as an array. These strings are embedded in a HTML template and that DOM structure may be repeated. Think of it as a table with each row/column having a different value. The keys are the column details (name,sortable etc.) and the method will fill in details of the cell.
The problem I'm having is speed.
Q1. How can I do this with a single expression?
Q2. Will I gain any speed?
Q3. Provided I cache the result, is readability preferred over a somewhat complicated regex?
Q4. Is there any way I can restructure the strings for a performance boost?
Ideally, I'd like to scan the string only once, convert it to PHP code, and do an eval
each time it needs to be used.