Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 解析字符串中的变量,如vBulletin_Php_Parsing_Vbulletin_Phrase - Fatal编程技术网

Php 解析字符串中的变量,如vBulletin

Php 解析字符串中的变量,如vBulletin,php,parsing,vbulletin,phrase,Php,Parsing,Vbulletin,Phrase,我不知道该怎么说。。 当试图解析字符串中的变量,比如短语中的VBuffin时,我应该考虑什么?p> 前 我检查了smarty/raintpl,并研究了一些正则表达式,所有这些,smarty/raintpl正是我需要的,唯一的问题是,它们都从文件中读取,我从数据库中提取,我需要这样的东西 $username = "Bob"; $html = "{username} you have two notifications"; display($html); 如果我使用任何替换函数regex/str\

我不知道该怎么说。。 当试图解析字符串中的变量,比如短语中的VBuffin时,我应该考虑什么?p> 前

我检查了smarty/raintpl,并研究了一些正则表达式,所有这些,smarty/raintpl正是我需要的,唯一的问题是,它们都从文件中读取,我从数据库中提取,我需要这样的东西

$username = "Bob";
$html = "{username} you have two notifications";
display($html);

如果我使用任何替换函数regex/str\u replace,这会降低脚本/网站的速度吗?

您可以使用简单的regex,如:

$matches = array();

$string = '{username} you have two notifications';
preg_match_all('/{(\w+)}/', $string, $matches);
print_r($matches);
哪些产出:

Array
(
    [0] => Array
        (
            [0] => {username}
        )

    [1] => Array
        (
            [0] => username
        )

)
或者如果你想要更高级的东西:

$matches = array();

$string = '{username or something=3} you have two notifications';
preg_match_all('/{([^}]+)}/', $string, $matches);
print_r($matches);
这同样给了你:

Array
(
    [0] => Array
        (
            [0] => {username or something=3}
        )

    [1] => Array
        (
            [0] => username or something=3
        )

)
我建议你尽可能地严格——也就是说,你确切地知道你想要允许什么,并且只允许那些,比如小写字母、数字和空格——从长远来看,这会使事情变得更简单


可以选择使用外部模板引擎,如smarty、twig或dwoo,但根据您的项目,您可能会发现这有点过分。

请考虑使用已完成此操作的模板引擎。例如:-但是变量语法与SmartyI稍有不同,如果您正在寻找模板系统,请考虑:Raintpl有一个非常相似的语法:{$variable_name}
Array
(
    [0] => Array
        (
            [0] => {username or something=3}
        )

    [1] => Array
        (
            [0] => username or something=3
        )

)