以下程式碼可以幫助您以陣列格式取得 docBlock 的內容,從 @ 符號開始,並忽略 (*) 星號。
class Home {
/**
*這個方法載入首頁
*@param int $id 使用者 ID
*@throws \Exception 如果使用者 ID 不存在
*@return void
*/
public function index( $id)
{
#...您的程式碼放在這裡
}
}
$object = new Home();
//取得註釋字串
$comment_string= (new ReflectionClass($object))->getMethod('index')->getdoccomment();
//定義用於字串比對的正規表示式模式
$pattern = "#(@[a-zA-Z]+\s*[a-zA-Z0-9, ()_].*)#";
//對提供的字串執行正規表示式
preg_match_all($pattern, $comment_string, $matches, PREG_PATTERN_ORDER);
echo "<pre>"; print_r($matches);
//輸出結果如下
陣列
(
[0] => 陣列
(
[0] => @param int $id 使用者 ID
[1] => @throws \Exception 如果使用者 ID 不存在
[2] => @return void
)
[1] => 陣列
(
[0] => @param int $id 使用者 ID
[1] => @throws \Exception 如果使用者 ID 不存在
[2] => @return void
)
)
//然後您可以透過索引存取特定的字串值