猿问

PHP 正则怎样获取php文件内的注释内容?

如图所示,我想要将注释拿下来生成api文档,各位大神帮帮忙!!!

江户川乱折腾
浏览 1169回答 7
7回答

喵喵时光机

/\*([\s\S]*?)\*/

米脂

根据 @老鼠拧刀满街找猫 提供的正则改的,生成txt文件的api文档。 正则: /(\/\*{2})([\s\S]*)(\*\/)/U $data = file_get_contents(dirname(__FILE__)."/WorkController.class.php"); preg_match_all('/(\/\*{2})([\s\S]*)(\*\/)/U',$data,$match); $file=fopen("api.txt","a"); foreach ($match[0] as $ma){ preg_match_all("/@.*?/U",$ma,$da); fwrite($file,"===================\r\n"); foreach ($da[0] as $d){ fwrite($file,$d."\r\n"); } fwrite($file,"===================\r\n\r\n\r\n"); } fclose($file);

慕尼黑5688855

推荐你直接使用swagger吧

犯罪嫌疑人X

反射就行了,然后取到注释之后,再使用正则提取你想要的各部分内容

慕容3067478

如果是这种/xxx/多行注释,直接 <?php $code = file_get_contents('index.php'); if (preg_match_all('|/\*(.*)\*/|isU', $code, $matches)){ print_r($matches); } 目测无问题

蝴蝶不菲

phpdoc
随时随地看视频慕课网APP
我要回答