如何通过textarea输入调用php中的预定义变量?就像{table}{/table}在预览时显示为table一样

如何通过textarea输入调用php中的预定义变量?就像{table}{/table}在预览时显示为table一样,php,parsing,variables,textarea,Php,Parsing,Variables,Textarea,我是编程新手,我不知道使用这种预期编程的功能或方法。 情况就是这样 PHP文件PHP上的预定义变量 <?php $a=1 ;$b=2;$c=3; $d=4; ?> 输出在php上进行echo时,解析{code}将被引用到预定义变量 I wanna buy 1 apple, 2 mangos, 3 grapes and 4 oranges 任何人都可以帮助我应该使用什么库或函数?谢谢也许一个简单的preg\u replace\u回调就可以了 <?php $values =

我是编程新手,我不知道使用这种预期编程的功能或方法。 情况就是这样

PHP文件PHP上的预定义变量

<?php $a=1 ;$b=2;$c=3; $d=4; ?>
输出在php上进行echo时,解析{code}将被引用到预定义变量

I wanna buy 1 apple, 2 mangos, 3 grapes and 4 oranges

任何人都可以帮助我应该使用什么库或函数?谢谢

也许一个简单的preg\u replace\u回调就可以了

<?php 
$values = array('a'=>1, 'b'=>2, 'c'=>3, 'd'=>4); // I don't want to litter the global namespace -> array
$userinput = 'I wanna buy {a} apple, {b} mangos, {c} grapes and {d} oranges';

$result = preg_replace_callback(
    '!{([a-z])}!', // limited to one small-case latin letter 
    function ($capture) use ($values) { 
        // for each match this function is called
        // use($values) "imports the array into the function
        // whatever this function returns replaces the match in the subject string
        $key = $capture[1];
        return isset($values[$key]) ? $values[$key] : '';
    },
    $userinput
);

echo $result;

你能告诉我你是什么意思吗?这发生在wordpress上(当创建post时)。用户类型代码,并作为预定义存储的数据发布
<?php 
$values = array('a'=>1, 'b'=>2, 'c'=>3, 'd'=>4); // I don't want to litter the global namespace -> array
$userinput = 'I wanna buy {a} apple, {b} mangos, {c} grapes and {d} oranges';

$result = preg_replace_callback(
    '!{([a-z])}!', // limited to one small-case latin letter 
    function ($capture) use ($values) { 
        // for each match this function is called
        // use($values) "imports the array into the function
        // whatever this function returns replaces the match in the subject string
        $key = $capture[1];
        return isset($values[$key]) ? $values[$key] : '';
    },
    $userinput
);

echo $result;
I wanna buy 1 apple, 2 mangos, 3 grapes and 4 oranges