【问题标题】:php regex : extract functions paramerts from code filesphp regex:从代码文件中提取函数参数
【发布时间】:2016-02-08 18:32:13
【问题描述】:

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

例子

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

我使用的wordpress gettext函数

_e , __

以及如何绕过类似的功能

use_e , get_email , select__


我通过正则表达式创建了这个函数,但它不是很有用

$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\(('|\")(.*?)('|\")\)/" ) ;

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

【问题讨论】:

  • 你是说你想用_e(搜索你的代码库吗?
  • 是的,还有其他gettext函数__
  • 你可以访问命令行/shell界面吗?
  • 不,我想用php代码,主机禁用所有命令功能
  • 那会很痛苦。在这种情况下,您将需要 PHP 打开所有文件,然后在内容上运行正则表达式。我会尝试看看您是否可以让您的主机允许您使用grep 或为您执行命令..

标签: php regex wordpress


【解决方案1】:

如果您的目标是生成主题的翻译目录,您应该使用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 shell,您可以使用简单的发行版(如 Ubuntu 或 Mint)设置 Linux VM,并在那里执行 xgettext 操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 2012-02-20
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多