1
我正在創建一個調試/ doc腳本,我想忽略所有的標準註釋,但留下PHPDoc的機智。清除所有評論,但離開PHPDoc
我使用的代碼是:
// Get file into an array line by line
$line = file($file);
// Trim all empty space and # or // comments
array_map(function($v) {
$v = trim($v);
$v = preg_split("/\/\/|#/", $v)[0];
return $v;
}, $line);
是問題,這將仍留在多線C風格的註釋,如:
/*
This
means
nothing
*/
因此,我相信我會讀取整個文件,找到每個/*
,同時忽略/**
(PHPDoc)並刪除所有內容,直到*/
。
所以正則表達式需要是多行的。我接受使用file()
的解決方案,但我認爲file_get_content()
在單獨的行上找到C風格的開放和關閉評論標籤會更實用。
試試這個 - 反射如果它的OOP。 http://stackoverflow.com/questions/22103019/php-reflection-get-constants-doc-comment,和http://php.net/manual/en/reflectionclass.getdoccomment.php – ArtisticPhoenix
@ArtisiticPhoenix,謝謝。這看起來像使用類/ token_get_all的一個很好的起點,就像akond所建議的那樣。 – Devon
是的,我寧願使用對token_get_all的反射僅僅因爲它增加了一些抽象,但我認爲只能使用token_get_all來完成,或者regx的你可以用regx和簡單的詞法分析器類型設置來做同樣的事情,但這是一個整個其他話題lol – ArtisticPhoenix