Extracting function names from a file (with or without regular expressions)

后端 未结 4 2016
南旧
南旧 2020-12-20 06:59

How can I extract all function names from a PHP file?

The file has many functions that look more or less like the following. Is a regular expression the best way to

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 07:29

    If the PHP file is syntactically correct and only contains function only, you can store the result of get_defined_functions into an array (anyway, is return an array).

    include_once the PHP file, compare the latest get_defined_functions with the previous array (like array_diff). Whichever results returned by array_diff are the functions define in the PHP file.

    If is an object declaration, the above won't work, although you can make use of function get_class_method, but it is not able to return protected and private methods.

    OR

    ob_start();
    highlight_string(file_get_contents('class.php'));
    $html = ob_get_contents();
    ob_end_clean();
    $dom = new DomDocument;
    $dom->loadHTML($html);
    

    You can have a list of DOM with a style attribute you can filter with.

提交回复
热议问题