如何在CodeIgniter中使用PHP Tokenizer读取PHP文件注释信息
Hey there! Let's walk through how to pull those @module and @author values from a PHP file's comments using PHP's Tokenizer extension within CodeIgniter. Your sample file has exactly the comment format we need to target, so here's a practical, working solution:
Core Idea
PHP's Tokenizer breaks down PHP code into "tokens"—we can loop through these tokens to find single-line comments (//), then use regex to yank out the values from our target tags.
Full Code Implementation (CodeIgniter Controller Example)
You can drop this into a controller, or wrap it into a helper for reuse across your project:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Comment_Tag_Extractor extends CI_Controller { // Main method to extract tags from a given file public function get_comment_tags($file_path) { // First, make sure the file exists if (!file_exists($file_path)) { return ['error' => 'Target file not found']; } // Grab the file content and split into tokens $file_content = file_get_contents($file_path); $tokens = token_get_all($file_content); // Initialize our result array $extracted_tags = [ 'module' => null, 'author' => null ]; // Loop through each token to find comments foreach ($tokens as $token) { // Check if this is a single-line comment token if (is_array($token) && $token[0] === T_COMMENT) { $comment_text = $token[1]; // Match @module and capture its value if (preg_match('/@module\s+(\w+)/i', $comment_text, $module_matches)) { $extracted_tags['module'] = $module_matches[1]; } // Match @author and capture its value if (preg_match('/@author\s+(\w+)/i', $comment_text, $author_matches)) { $extracted_tags['author'] = $author_matches[1]; } } } return $extracted_tags; } // Test this method with your sample file public function test_extraction() { // Replace this path with your actual file location // APPPATH is CodeIgniter's built-in constant for the application folder $target_file = APPPATH . 'path/to/your/sample-file.php'; $result = $this->get_comment_tags($target_file); // Print the result for debugging echo '<pre>'; print_r($result); echo '</pre>'; // For your sample file, this will output: // Array ( [module] => core [author] => VR ) } }
Key Details Explained
token_get_all(): This function parses the PHP file into an array of tokens. Each token is either a single character (like<?phpdelimiters) or an array containing the token type, content, and line number.T_COMMENT: This token type represents single-line comments (the//style used in your sample). If you were dealing with multi-line docblocks (/** ... */), you'd useT_DOC_COMMENTinstead.- Regex Patterns: The regex
/@module\s+(\w+)/imatches the@moduletag, skips any whitespace, and captures the following word (your module name). Theiflag makes it case-insensitive, so@Moduleor@MODULEwould also work. - CodeIgniter Paths: Using
APPPATHensures you're using a reliable, framework-aware path to your file instead of hardcoding absolute paths.
Quick Notes
- Make sure the Tokenizer extension is enabled in your PHP setup (it's enabled by default in most environments, but double-check if you run into issues).
- If your file has multiple instances of the same tag, this code will capture the last one. To collect all instances, just modify the result array to store values in an array instead of a single string.
- For better reusability, you can turn this into a CodeIgniter helper by moving the
get_comment_tags()method into a helper file and loading it where needed.
内容的提问来源于stack exchange,提问作者VR Patel
相关产品推荐
相关产品推荐

