2016-07-04 26 views
1

我有一个多语言网站存储translatables内的default.php填充包含所有键的数组。PHP自动检测translatables /检测一段代码正则表达式

我宁愿让它自动。 我已经有一个(单身)类,它能够根据类型检测我的所有文件。 (控制器,动作,视图,模型,等等)

我想检测任何一段代码,其格式是这样的:

$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 

ofcourse [a-zA-Z]是一个正则表达式的例子。

就像我sais我已经有一个类,检测某些文件。这个类也使用反射(或者在这种情况下,Zend反射,因为我使用Zend)但是我看不到一种方法来反映使用正则表达式的函数。

该动作将被放置在一个cronjob和手动称为动作,所以它不是一个大问题,当使用的内存有点太“大”。

+0

您可以编辑您的问题,以显示例如字符串你正在寻找匹配 –

+0

它已经在那里,$此,翻译( '[A-ZA-Z]' ); 该字符串会有所不同,但我总是试图保持它像这样 _ _ 。例如'Controller_Default_Welcome' – IMarks

回答

2

说明

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

Regular expression visualization

**要看到图像更好,只需右键点击在新窗口中的图像,然后选择查看

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

  • code block开始$this-translate('通过它KS收盘');
  • 地方的价值'引号内为捕获组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 \1: 
---------------------------------------------------------------------- 
    (?:      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 \1 
---------------------------------------------------------------------- 
    '      '\'' 
---------------------------------------------------------------------- 
    [)]      any character of: ')' 
---------------------------------------------------------------------- 
    ;      ';' 
---------------------------------------------------------------------- 
+0

我在使用以下代码时preg_match_all(“/ [$] this-> translate [(]'((')]出现以下错误'preg_match_all():编译失败: ?:[^'\\] | \\。|'')*)'[]];/mix“,$ this-> content,$ matches);' 编辑:明白了'preg_match_all(”/ [$] this-translate [(]'((?:[^'\\\\] | \\\\。|'')*)'[]]/mix“,$ this-> content,$ matches );' 但preg_match_all不会返回$ matches中的结果; – IMarks

+0

在你的第一个例子中,你有'this-> translate',而在你的第二个例子中你有'this-translate'。 “>”字符重要吗? –

+0

是的,缺少>是一个错误,>,或者说更好的说 - >是调用translate()的方法。我以为我把它全部固定在我的代码中,剩下一点,_shame_。感谢您的帮助 – IMarks