Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search in multiple .php files?

Tags:

php

search

I have around 100 .php files in one directory and I'm looking for one small function, what's the fastest way to search trough all contents of these files?

[edit]

I'm using Windows 7 Ultimate/NuSphere PhpED.

like image 324
Wordpressor Avatar asked Aug 25 '11 23:08

Wordpressor


People also ask

How do I browse PHP files?

You can open current file in browser using following methods: Click the button Open In Browser on StatusBar. In the editor, right click on the file and click in context menu Open PHP/HTML/JS In Browser. Use keybindings Shift + F6 to open more faster (can be changed in menu File -> Preferences -> Keyboard Shortcuts )

How do I get a list of files in a directory in PHP?

The scandir() function returns an array of files and directories of the specified directory.

How do I link two PHP files?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

How do I search for a word in PHP?

php $searchthis = "mystring"; $matches = array(); $handle = @fopen("path/to/inputfile. txt", "r"); if ($handle) { while (! feof($handle)) { $buffer = fgets($handle); if(strpos($buffer, $searchthis) !== FALSE) $matches[] = $buffer; } fclose($handle); } //show results: print_r($matches); ?>


3 Answers

Try this:

<?php
function getFilesWith($folder, $searchFor, $extension = 'php') {

    if($folder) {
        $foundArray = array();
        // GRAB ALL FILENAMES WITH SUPPLIED EXTENSION
        foreach(glob($folder . sprintf("*.%s", $extension)) as $file) {
            $contents = file_get_contents($file);

            if(strpos($contents, $searchFor) !== false) {
                $foundArray[] = $file;
            }
        }

        if(count($foundArray)) {
            return $foundArray;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

$matched_files = getFilesWith('path/to/folder', 'Looking for this');
?>
like image 175
AVProgrammer Avatar answered Nov 05 '22 00:11

AVProgrammer


Use your php editors "find in files" function.

Invaluable.

edit PHPNuSphere totally supports this. you need to learn some google fu brother.

If your editor doesn't have this, you need to switch ASAP. https://stackoverflow.com/search?q=php+editor

Find in files in Visual Studio

Find in files in Notepad++

like image 40
Byron Whitlock Avatar answered Nov 05 '22 01:11

Byron Whitlock


Install cgywin - then you can use grep!

like image 28
Ed Heal Avatar answered Nov 05 '22 00:11

Ed Heal