Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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字符串中的所有变量?_Php_Arrays_String - Fatal编程技术网

是否有一个函数可以获取给定php字符串中的所有变量?

是否有一个函数可以获取给定php字符串中的所有变量?,php,arrays,string,Php,Arrays,String,我有一个脚本,它包含一个双引号字符串(如果有区别的话,可以用来创建一个html表),其中包含大约12个变量。每个变量都由while循环修改。我想在循环的每次迭代中将每一组值推送到多维数组中。现在我可以做 array\u push($my\u array,$var1,$var2,$var3,…) 但这很尴尬 是否有方法将此字符串中的所有变量转储到我的数组中,例如: array_push($my_array,get_vars_from_string($string)) (显然,如果脚本中使用的变量一

我有一个脚本,它包含一个双引号字符串(如果有区别的话,可以用来创建一个html表),其中包含大约12个变量。每个变量都由while循环修改。我想在循环的每次迭代中将每一组值推送到多维数组中。现在我可以做
array\u push($my\u array,$var1,$var2,$var3,…)

但这很尴尬

是否有方法将此字符串中的所有变量转储到我的数组中,例如:
array_push($my_array,get_vars_from_string($string))

(显然,如果脚本中使用的变量一开始就在一个数组中,那会很好,但我没有编写原始的和需要对程序结构进行太多更改的更改。)


“字符串中的变量”我的意思是:
$table=“$var1$var2 stuff…”


你会明白我所说的标记化部分是什么意思。有趣的部分是
${$key}
。若要从字符串中获取变量,可以使用,但必须使用php未处理的字符串,而是将该字符串分配给变量的原始代码行

具有不良实践的概念验证代码:

<?php

$string_vars = array();
$double_quote_started = false;
$all_vars = array();
$table = "";
$var1 = 5;
$var2 = -5;

while($var1-- && $var2++) {

// note single quotes, this does not evaluate variables but treats everything as string
$table_str = <<<'EOT'
$table .= "<td>$var1</td><td>$var2</td><td>funky stuff</td>";
EOT;
    //evaluate the line as it was earlier
    eval($table_str);

    // since this is is first iteration, let's search our string for them
    if (empty($string_vars)) {
        foreach(token_get_all("<?php " . $table_str) as $token) {
            if (is_array($token) && $token[0] == T_VARIABLE && $double_quote_started) {
                $string_vars[] = substr($token[1],1);
            } elseif ($token === '"') {
                $double_quote_started = !$double_quote_started;     
            }
        }
    }
    $this_iteration = array();
    foreach($string_vars as $var){
        // variable variable to get content of variable
        $this_iteration[$var] = $$var;
    }
    // save this iteration vars
    $all_vars[] = $this_iteration;
}

字符串中的变量?你能展示一个例子吗?你的意思是什么?你想把变量的内容或变量名推到数组中吗?你能发布这个循环现在看起来是什么样子的代码吗?不,因为字符串中的变量已经在当前范围内设置。因为你在问题注释中发布了一个示例字符串。我认为您需要使用preg_match来查找变量名,我认为explode不起作用,除非它是一个严格格式的字符串。另外,如果它是某种用户输入,请确保您对该数据进行清理。这里是explode和option,我也同意bonez在这方面使用preg_match
<?php

$string_vars = array();
$double_quote_started = false;
$all_vars = array();
$table = "";
$var1 = 5;
$var2 = -5;

while($var1-- && $var2++) {

// note single quotes, this does not evaluate variables but treats everything as string
$table_str = <<<'EOT'
$table .= "<td>$var1</td><td>$var2</td><td>funky stuff</td>";
EOT;
    //evaluate the line as it was earlier
    eval($table_str);

    // since this is is first iteration, let's search our string for them
    if (empty($string_vars)) {
        foreach(token_get_all("<?php " . $table_str) as $token) {
            if (is_array($token) && $token[0] == T_VARIABLE && $double_quote_started) {
                $string_vars[] = substr($token[1],1);
            } elseif ($token === '"') {
                $double_quote_started = !$double_quote_started;     
            }
        }
    }
    $this_iteration = array();
    foreach($string_vars as $var){
        // variable variable to get content of variable
        $this_iteration[$var] = $$var;
    }
    // save this iteration vars
    $all_vars[] = $this_iteration;
}