I am trying to implement an admin interface where a manager can create advanced rules for creating site meta tags.
I have a function that takes a template and replaces the placeholders in it with the values ββfrom the $ registry and applies modifiers if necessary.
$registy = array( 'profession_name' => 'actor', 'total_found' => 100, ); $result = preg_replace_callback('/\{([^{}:]+)(:[^{}:]+)?\}/', function($matches) use ($registry) { $result = $registry[$matches[1]] ?: $matches[0]; if (in_array($matches[2], array(':ucfirst', ':strtolower', ':strtoupper', ':lcfirst'))) { $result = call_user_func(substr($matches[2], 1), $result); } return $result; }, $template);
Example:
Profession {name: ucfirtst} is {profession_name: strtoupper}
divided into
Profession Name - ACTOR
I am trying to implement the conditions, so one could make the following rules:
text with {placeholdes} before the condition. [{total_found}, TRUE text with {placeholders}, FALSE text with {placeholders}], ending text with {placeholders} after the condition.
Thus, my function will get the value {total_found} from the registry and depending on whether it returns true or false a string with the corresponding false or true-pattern. I am very poor at regular expressions and cannot figure out how to write this condition in regular expressions. Help me please.
UPDATE: a concrete example looks like whis:
$template = 'Profession is {profession_name}. [{total_found}, {total_found} vacancies found, No vacancies found]. {profession_name} in your town';
in my callback function, I would set a condition like this
if ($matches[condition]) // i don't no how exactly $result = $matches[true-condition]; else $result = $matches[false-condition]
So, in the case of $registry['total_found'] = 100; the end result will be "Profession is an actor. 100 vacancies found. An actor in your city." In the case of $registry['total_found'] = 0; the end result is "The profession is an actor. No vacancies found. An actor in your city."