phpregex:从代码文件中提取函数参数


php regex : extract functions paramerts from code files

我想获取我所有的wordpress插件,然后获取所有gettext函数参数

示例

 die(_e('Error')) ;
 die( '<b>'._e('Error').'</b>' ) ;
 _e('Error')

wordpress gettext的函数我使用

_e__

以及如何绕过类似功能

使用_e获取_email选择__


我用regex创建了这个函数,但它对不是很有用

$list_words = array();
function po_fetch_code($dir , $preg = "/_'(('|'")(.*?)('|'")')/"){
     if( is_dir($dir) ){
           $list = scandir($dir) ;
           foreach($list as $l){
              if($l == '.' || $l == '..'){
                // Do Nothing
              }else{
                po_fetch_code($dir.'/'.$l , $preg) ;
              }
           }
     }else{
          $ext = strrchr($dir , '.') ;
          //if($ext == '.php' || $ext == '.html' || $ext == 'html'){
               //$cont = file_get_contents($dir) ;
               $list = file($dir) ;
               global $list_words ;
               foreach($list as $key => $l){
                  preg_match_all( $preg , $l , $matches );
                  if(!empty($matches[2])){
                        foreach($matches[2] as $k=>$m){
                                 //$m = str_replace(array( "''",'''' , '"' , "'",'''' ), '' ,$m) ;
                                 //$m = str_replace('''s', 's' ,$m) ;
                                 if(!isset($list_words[$m])){
                                     $list_words[$m]['msgid'] = array($m) ;
                                     $list_words[$m]['msgstr'] = array($m) ;
                                     $list_words[$m]['reference'] = array() ;
                                  }
                                  $list_words[$m]['reference'][] = $dir.':'.$key ;
                        }
                  }
               }
         // }
     }
}
$dir = 'themes/mytheme/' ;
po_fetch_code($dir , "/__'(('|'")(.*?)('|'")')/" ) ;
po_fetch_code($dir , "/_e'(('|'")(.*?)('|'")')/" ) ;

有什么方法可以增强我的功能吗??感谢

如果您的目标是生成主题的翻译目录,则应该使用xgettext命令行工具。尝试手动解析PHP文件是没有意义的。

## first, collect all the theme’s PHP files into one temporary file
find -name "*.php" wp-content/themes/yourtheme > /tmp/themefiles.txt
# create catalog from translatable strings in your theme’s files
xgettext --from-code=utf-8 --keyword=__ --keyword=_e --keyword="_n:1,2" --keyword="_x:2c,1" --language=PHP -j -o wp-content/themes/yourtheme/yourtheme.pot -f /tmp/themefiles.txt

如果你不能访问网络服务器上的Linux外壳,你可以用一个简单的发行版(如Ubuntu或Mint)建立一个Linux虚拟机,并在那里做xgettext的事情。