Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8750427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:51:41+00:00 2026-06-13T12:51:41+00:00

I have a python script I wrote that I need to port to php.

  • 0

I have a python script I wrote that I need to port to php. It recursively searches a given directory and builds a string based on regex searches. The first function I am trying to port is below. It takes a regex and a base dir, recursively searches all files in that dir for the regex, and builds a list of the string matches.

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

I never use PHP except for basic GET param manipulation. I grabbed some directory walking code from the web, and am struggling to make it work like the python function above due to my utter lack of the php API.

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";
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:51:42+00:00Added an answer on June 13, 2026 at 12:51 pm

    Here is my solution:

    <?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>
    

    Be aware that:

    • It reads each file to search for the pattern, so it may require a lot of memory (check the “memory_limit” configuration in your PHP.INI file).
    • It does’nt work with unicode files. If you are working with unicode files you should use the “mb_ereg_match” function rather than the “preg_match” function.
    • It does’nt follow symbolic links

    In conclusion, even if it’s not the most efficient solution at all, it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a Python script that I need to have installed on Windows Server
I've wrote a python script that need to pass millions of items to a
I have a python modules question: I need to write a script that will
I have a python script that calls a USB-based data-acquisition C# dotnet executable. The
I have a python script that calls a USB-based data-acquisition C# dotnet executable. The
I have a small Python script that I need to modify because the format
I have a python script that runs a program, which generates few .exe files
I have a python script that I want always to run in the background.
I have a Python script that makes use of 'Print' for printing to stdout.
I have a python script running on a small server that is called in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.