2016-07-04 28 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