I'm sure that subject probably confused some of you, though I can't think of another quick way to describe it.
It's not a template, as in creating a page template - like most template systems out there, but it has some of those parsing similarities.
I am basically creating a dynamic question and answer system, where the question, answer, and possibly an object definition are stored in a database.
I've got most of it working, but have come across a road-block as I'm trying to implement a way for the definition to include a function call.
Here are the rules I've come up with so far:
Variables are all random numbers
Variables are identified by $.
Variables in the answer definition must be defined in the question or they are ignored.
Variables are defined with a $ in front of the name and curly brackets surrounding a number {#} (0 to #) or two numbers {#,#} (# to #).
Function definitions are identified by a # in front.
Functions may have multiple parameters.
Function parameters are within parenthesis.
The answer needs to go through eval for mathematical evaluation questions to work. This is the LAST thing that gets run:
Here is an example of what I'm doing (stuff that works):
That ends up after being parsed as:
parsed a: mt_rand(1, 9)
parsed b: mt_rand(1, 9)
$q = 'What is 4 + 3?'
$a = '7'
$q = 'What is 13 - 6?'
$a = '7'
Those examples work fine. Here is where it gets complicated.
Parsing the question works just fine the question ends up being something like:
What is the 4th word of the following sentence?
or
What is the fourth word of the following sentence?
I haven't even started on the regex to find to nth word yet, I'll worry about the parsing first.
I would like to accurately find the function name (including the ::, though the function doesn't have to have it) and the parameters.
Here's what I have currently for parsing the functions:
That ends up with an array with the function name as the key and the parameters as a subarray, for example:
Thanks.
It's not a template, as in creating a page template - like most template systems out there, but it has some of those parsing similarities.
I am basically creating a dynamic question and answer system, where the question, answer, and possibly an object definition are stored in a database.
I've got most of it working, but have come across a road-block as I'm trying to implement a way for the definition to include a function call.
Here are the rules I've come up with so far:
Variables are all random numbers
Variables are identified by $.
Variables in the answer definition must be defined in the question or they are ignored.
Variables are defined with a $ in front of the name and curly brackets surrounding a number {#} (0 to #) or two numbers {#,#} (# to #).
Function definitions are identified by a # in front.
Functions may have multiple parameters.
Function parameters are within parenthesis.
The answer needs to go through eval for mathematical evaluation questions to work. This is the LAST thing that gets run:
Code:
if( substr($a, -1) != ';' )
$a .= ';';
$a = '$pDataResult = '.$a;
eval($a);
$a = $pDataResult;
Here is an example of what I'm doing (stuff that works):
Code:
$q = 'What is $a{1,9} + $b{1,9}?';
$a = '$a + $b';
parsed a: mt_rand(1, 9)
parsed b: mt_rand(1, 9)
$q = 'What is 4 + 3?'
$a = '7'
Code:
$q = 'What is $a{10,18} - $b{1,9}?';
$a = '$a - $b';
$a = '7'
Those examples work fine. Here is where it gets complicated.
Code:
$q = 'What is the #DynamicQ::num2RandOrd($a{1,6}) word of the following sentence?';
$a = '#DynamicQ::RegExMatch(parameters here)';
$o = 'This is a very common sentence.';
What is the 4th word of the following sentence?
or
What is the fourth word of the following sentence?
I haven't even started on the regex to find to nth word yet, I'll worry about the parsing first.
I would like to accurately find the function name (including the ::, though the function doesn't have to have it) and the parameters.
Here's what I have currently for parsing the functions:
Code:
function Parse4Funcs($txt){
$funcs = array();
preg_match_all('/#([a-zA-Z0-9\:]*?)\((.*?)\)/', $txt, $matches);
for($i = 0; $i < count($matches[1]); $i++){
$args = split(',', $matches[2][$i]);
$funcs[$matches[1][$i]] = $args;
}
return $funcs;
}
Code:
Array(
'DynamicQ::num2RandOrd' => Array(
4
)
)
Thanks.