You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CodeIgniter中使用PHP Tokenizer读取PHP文件注释信息

Extracting Comment Tags (@module, @author) in CodeIgniter Using PHP Tokenizer

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 <?php delimiters) 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 use T_DOC_COMMENT instead.
  • Regex Patterns: The regex /@module\s+(\w+)/i matches the @module tag, skips any whitespace, and captures the following word (your module name). The i flag makes it case-insensitive, so @Module or @MODULE would also work.
  • CodeIgniter Paths: Using APPPATH ensures 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:06:22