I need a function that could generate a regex. For example if I write
$pat = "My {$var} is {$var2}"
to generate a regex that when I use
preg_match($pat,"My name is Kevin",$matches);
to return
$matches[1] = 'name';
$matches[2] = 'Kevin';
and the the string could be : "My $nameword is $name and i'm $age years old"
And the problem is:
FIG1
<?php
function gen_pattern($string)
{
<--- add code for generating pattern --->
}
?>
FIG2
$pat = gen_pattern('my name is {$X}');
ereg($pat,'my name is moo',$regs);
//$regs[1] == "moo„
([^\s]*)
$pat = gen_pattern('my {$YZ} is {$IJK}');
ereg($pat,'my name is moo',$regs);
//$regs[1] == "name", $regs[2] == "moo"
My homework's description is as follows:
Figure 1 shows a function which can generate a regular expression, usable for the ereg function.
Figure 2 shows how the pattern should work. In the given string, each item {$ABC} represents a placeholder, i.e. {$X} is the single place holder in "my name is {$X}", and there are two place holders in "my {$YZ} is {$IJK}". A place holder can be of any length above zero.
In the resulting pattern, each place holder represents one single word of the text, which has to be added to the result list. I.e. {$X} represents the next word after "my name is". As a result, $regs[1] becomes "moo" if the pattern is used on text "my name is moo". Assume that we have only texts of form [a-z ]* (note the white space).
My Task:
Fill in the green spot in Figure 1. Dont bother with error handling or checking the correct syntax of given string (i.e. "my name is {{}" or similar invalid input will not occur).
And I don't know where to begin.
ereg*family of functions is deprecated. You should not use them anymore. use thepreg_*family of functions instead.