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

how to call predefined variable in php through textarea input? just like {table}{/table} shown as table when previewed

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

PHP FILE php

上的预定义变量
<?php $a=1 ;$b=2;$c=3; $d=4; ?>

HTML FILE 在文本区域输入 在文本区域的顶部写着

use code {a} to generates 1, {b} to generates 2, {c} to generates 3, {d} to generates 4

所以当用户在下方输入时:

I wanna buy {a} apple, {b} mangos, {c} grapes and {d} oranges

OUTPUT 以某种方式在 php 上回显时,解析 {code} 将引用预定义变量

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

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

也许一个简单的 preg_replace_callback 就可以了

<?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