从文件中提取函数名(带或不带正则表达式) [英] Extracting function names from a file (with or without regular expressions)
本文介绍了从文件中提取函数名(带或不带正则表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从PHP文件中提取所有函数名称?
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 do it?
public function someFunction(
private function anotherOne(
推荐答案
你可以用正则表达式找到它们:
You could find them with a regular expression:
# 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];
}
这篇关于从文件中提取函数名(带或不带正则表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文