PHP 自动检测可译物/通过正则表达式检测一段代码

PHP autodetect translatables / detect piece of code by regex

我有一个多语言网站,它在一个 default.php 中存储可翻译的内容,其中填充了一个包含所有键的数组。

我希望将其设为自动。 我已经有一个(单例)class,它能够根据类型检测我的所有文件。 (控制器、动作、视图、模型等...)

我想检测任何格式如下的代码:

$this->translate('[a-zA-Z]');
$view->translate('[a-zA-Z]');
getView()->translate('[a-zA-Z]');
throw new Exception('[a-zA-Z]');
addMessage(array('message' => '[a-zA-Z]');

但是启动时必须过滤with/contains:

$this->translate('((0-9)+_)[a-zA-Z]');
$this->translate('[a-zA-Z]' . $* . '[a-zA-Z]'); // Only a variable in the middle must filtered, begin or end is still allowed

当然 [a-zA-Z] 是一个正则表达式示例。

就像我说的那样,我已经有一个 class 可以检测某些文件。 class 也使用反射(或者在本例中为 Zend 反射,因为我使用的是 Zend)但是我看不到使用正则表达式来反射函数的方法。

该动作将被放置在一个 cronjob 和手动调用的动作中,因此当使用的内存有点 'too' 大时这不是一个大问题。

描述

[$]this->translate[(]'((?:[^'\]|\.|'')*)'[)];

** 要更好地查看图像,只需右键单击图像并 select 在新 window

中查看

此正则表达式将执行以下操作:

  • 代码块从 $this-translate(' 开始到结束 ');
  • ' 引号内的值放入捕获组 1
  • 避免混乱的边缘情况,在这种情况下,子字符串中可能包含看起来像结尾的 '); 字符串,而实际上字符可以被转义。

例子

现场演示

https://regex101.com/r/eC5xQ6/

示例文本

$This->Translate('(?:Droids\');{2}');
$NotTranalate('fdasad');
$this->translate('[a-zA-Z]');

样本匹配

MATCH 1
1.  [17-33] `(?:Droids\');{2}`

MATCH 2
1.  [79-87] `[a-zA-Z]`

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  [$]                     any character of: '$'
----------------------------------------------------------------------
  this->translate           'this->translate'
----------------------------------------------------------------------
  [(]                      any character of: '('
----------------------------------------------------------------------
  '                        '\''
----------------------------------------------------------------------
  (                        group and capture to :
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      [^'\]                   any character except: ''', '\'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      \                       '\'
----------------------------------------------------------------------
      .                        any character except \n
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      ''                       '\'\''
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
  )                        end of 
----------------------------------------------------------------------
  '                        '\''
----------------------------------------------------------------------
  [)]                      any character of: ')'
----------------------------------------------------------------------
  ;                        ';'
----------------------------------------------------------------------