如何从 PHP 文件中提取所有函数名称?
该文件有许多函数,大致如下所示。正则表达式是最好的方法吗?
public function someFunction(
private function anotherOne(
最佳答案
您可以使用正则表达式找到它们:
# The Regular Expression for Function Declarations
$functionFinder = '/function[\s\n]+(\S+)[\s\n]*\(/';
# Init an Array to hold the Function Names
$functionArray = array();
# Load the Content of the PHP File
$fileContents = file_get_contents( 'thefilename.php' );
# Apply the Regular Expression to the PHP File Contents
preg_match_all( $functionFinder , $fileContents , $functionArray );
# If we have a Result, Tidy It Up
if( count( $functionArray )>1 ){
# Grab Element 1, as it has the Matches
$functionArray = $functionArray[1];
}
关于php - 从文件中提取函数名称(带或不带正则表达式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4622390/