【问题标题】:How can I read file metadata in PHP? [closed]如何在 PHP 中读取文件元数据? [关闭]
【发布时间】:2021-02-21 13:02:11
【问题描述】:

如何动态读取PHP文件中的注释块注解?

例子:-

<?php
/**
* @author John Doe (jdoe@example.com)
* @copyright MIT
*/

如何使用另一个 php 文件获取@author?

【问题讨论】:

标签: php


【解决方案1】:

不幸的是,不要认为有任何“好方法”可以做到这一点。只需正则表达式,例如

<?php
/**
* @author John Doe (jdoe@example.com)
* @copyright MIT
*/
/**
* @author Moe (moe@example.com)
* @copyright MIT
*/

$php_file = __FILE__;
$content = file_get_contents($php_file);
$rex = <<<'REX'
/\n\*\s*\@author\s*(?<author>[^\n]+?)\s*?$/m
REX;
$authors = [];
if(preg_match_all($rex,$content,$matches)){
    $authors += $matches["author"];
}
var_dump($authors);

即使这样也不是特别可靠,但特定的脚本会产生

array(2) {
  [0]=>
  string(27) "John Doe (jdoe@example.com)"
  [1]=>
  string(21) "Moe (moe@example.com)"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2018-01-18
    • 2015-03-23
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多