php - 从 Python 到 PHP 的 GREP 函数

标签 php python

我有一个我写的 python 脚本,我需要移植到 php。它递归地搜索给定的目录并基于正则表达式搜索构建一个字符串。我尝试移植的第一个功能如下。它需要一个正则表达式和一个基本目录,递归地在该目录中的所有文件中搜索正则表达式,并构建一个字符串匹配列表。

def grep(regex, base_dir):
    matches = list()
    for path, dirs, files in os.walk(base_dir):
        for filename in files:
            fullpath = os.path.join(path, filename)
            with open(fullpath, 'r') as f:
                content = f.read()
                matches = matches + re.findall(regex, content)
    return matches

除了基本的 GET 参数操作外,我从不使用 PHP。我从网上抓取了一些目录遍历代码,由于我完全缺乏 php API,我正在努力让它像上面的 python 函数一样工作。

function findFiles($dir = '.', $pattern = '/./'){
  $prefix = $dir . '/';
  $dir = dir($dir);
  while (false !== ($file = $dir->read())){
    if ($file === '.' || $file === '..') continue;
    $file = $prefix . $file;
    if (is_dir($file)) findFiles($file, $pattern);
    if (preg_match($pattern, $file)){
      echo $file . "\n";
    }
  }
}

最佳答案

这是我的解决方案:

<?php 

class FileGrep {
    private $dirs;      // Scanned directories list
    private $files;     // Found files list
    private $matches;   // Matches list

    function __construct() {
        $this->dirs = array();
        $this->files = array();
        $this->matches = array();
    }

    function findFiles($path, $recursive = TRUE) {
        $this->dirs[] = realpath($path);
        foreach (scandir($path) as $file) {
            if (($file != '.') && ($file != '..')) {
                $fullname = realpath("{$path}/{$file}");
                if (is_dir($fullname) && !is_link($fullname) && $recursive) {
                    if (!in_array($fullname, $this->dirs)) {
                        $this->findFiles($fullname, $recursive);
                    }
                } else if (is_file($fullname)){
                    $this->files[] = $fullname;
                }
            }
        }
        return($this->files);
    }

    function searchFiles($pattern) {
        $this->matches = array();
        foreach ($this->files as $file) {
            if ($contents = file_get_contents($file)) {
                if (preg_match($pattern, $contents, $matches) > 0) {
                    //echo $file."\n";
                    $this->matches = array_merge($this->matches, $matches);
                }
            }
        }
        return($this->matches);
    }
}


// Usage example:

$fg = new FileGrep();
$files = $fg->findFiles('.');               // List all the files in current directory and its subdirectories
$matches = $fg->searchFiles('/open/');      // Search for the "open" string in all those files

?>
<html>
    <body>
        <pre><?php print_r($matches) ?></pre>
    </body>
</html>

请注意:

  • 它读取每个文件以搜索模式,因此它可能需要大量内存(检查 PHP.INI 文件中的“memory_limit”配置)。
  • 它不适用于 unicode 文件。如果您使用的是 unicode 文件,则应使用“mb_ereg_match”函数而不是“preg_match”函数。
  • 它不遵循符号链接(symbolic link)

总而言之,即使它根本不是最有效的解决方案,它也应该有效。

关于php - 从 Python 到 PHP 的 GREP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13123568/

相关文章:

php - 提交表格的问题

php - 在 Woocommerce 单一产品页面中显示特定产品标签的自定义内容

Python - Flask 和 werkzeug - 继续提供 "BadRequestKeyError: 400 Bad Request: KeyError: ' 文件'"

python - 向 Pandas DataFrame 添加一个新列,并使用来自单独 DataFrame 的编码数据而不使用循环?

python - 为什么在 python 中调用 file.read 会使我的文件充满垃圾?

python - 绘制 Pandas 数据时如何禁用标签?

python - 两幅图像之间的特征匹配和检测

具有垂直选项卡内容的 Php 数组

php - 将 Session 变量传递给称为 PHP 脚本的 ajax

php - 找不到保存处理程序 - Redis 集群