How to list all PHP variables in a file?

后端 未结 3 1530
不知归路
不知归路 2021-01-07 11:53

I have a PHP file with PHP Variables inside.
Example:

Hi ,
Can you please send me an email ?

I would like to

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-07 12:34

    function extract_variables($content, $include_comments = false)
    {
        $variables = [];
    
        if($include_comments)
        {
            preg_match_all('/\$[A-Za-z0-9_]+/', $content, $variables_);
    
            foreach($variables_[0] as $variable_)
                if(!in_array($variable_, $variables))
                    $variables[] = $variable_;
        }
        else
        {
            $variables_ = array_filter
            (
                token_get_all($content),
                function($t) { return $t[0] == T_VARIABLE; }
            );
    
            foreach($variables_ as $variable_)
                if(!in_array($variable_[1], $variables))
                    $variables[] = $variable_[1];
        }
    
        unset($variables_);
        return $variables;
    }
    
    // --------------
    
    $content = file_get_contents("file.php");
    $variables = extract_variables($content);
    
    print_r($vars[0]);
    
    // --------------
    

提交回复
热议问题