13行代码实现:PHP读取JS文件并正则提取函数及其参数
再得到可以console.log函数名及参数的内容的插入到Js文件对应函数下一行
让浏览器调试的时候显示相关函数名及显示传递的相关参数值
<?php
$content = file_get_contents('chalide.js');
$pattern = '/function\s+(\w+)\s*\(([^\)]*)\)\s*\{/';
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$yuan = $match[0]; $name = $match[1]; $params = " ".$match[2];
if(Trim($params)=="") $params = "'$name'";
$log = "console.log('$name', $params);";
$content = str_replace($yuan,"$yuan\r\n$log\r\n",$content);
echo "$yuan => $log\n";
}
file_put_contents("chalide.new.js",$content);
?>
功能举例(=>前提取的函数,=>后是该函数后插入内容):
function a(b, c) { => console.log('a',b, c);
function A(B) { => console.log('A',B);
function D() { => console.log('D','D');
两个或多个参数则显示函数名及多个参数
一个参数情况则显示函数名及一个参数
无参数则重复显示函数名