php - 从文件中提取函数名称(带或不带正则表达式)

标签 php regex function

如何从 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/

相关文章:

php - 谁能引导我找到正则表达式的 DEFINE 断言文档?

javascript - 匹配 OData 查询的正则表达式

c# - 提取文本文件中特定标签之间的内容 - C#

python - 改进我的 python 函数解析 Wordpress/config.php 的建议

php - 如何使用 mysqli API 制作一个完全动态的准备语句?

php - 无法在 ubuntu 14.04 上重启 apache2 服务器

android - 如何在 kotlin 中将函数作为参数传递 - Android

android - 使用 onClick 按钮更改 EditText 的值?

php - 放置请求不起作用,Laravel 4

php - 模式匹配统一的句子结构