1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Search help

Discussion in 'PHP' started by sitescripts, Nov 14, 2012.

  1. #1
    hi Im trying to make a script that will recursively search files in a directory and all its sub-directories for a string
    can any one help?
     
    sitescripts, Nov 14, 2012 IP
  2. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #2
    Here you go friend:

    
    
    <?php
    $res = array();
    $directory = $_SERVER['DOCUMENT_ROOT'].'/path/to/start/searching/';
    $arg = opendir($directory);
    while ($file = readdir($arg)) {
              if ($file != "." && $file != "..") {
                        if(preg_match('/substring/'), $file)) {
                                  $res[] = $file;
                        }
              }
    }
    ?>
    
    
    PHP:
     
    DomainerHelper, Nov 14, 2012 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    I've modified your code due the fact the TS asked for searching withing subdirectories and content, this altered version will search in subdirectories from given path for given search parameters

    
    <?php
    
    function startSearching($path, $search, &$return = array())
    {
        $arg = opendir($path);
        while ($file = readdir($arg)) 
        {
            if ($file != "." && $file != "..") 
            {
                if (is_dir($file))
                {
                    $return = startSearching($file,
                                             $search,
                                             $return);
                }
                elseif (preg_match('/' . $search . '/'), $file)) 
                {
                    $return[] = $file;
                }
            }
        }
        
        return $return;
    }
    
    print_r(startSearching($_SERVER['DOCUMENT_ROOT'].'/path/to/start/searching/',
                           "searchstring");
    
    ?>
    
    PHP:
    Just out of the head, not tested! :)
     
    EricBruggema, Nov 14, 2012 IP
  4. sitescripts

    sitescripts Active Member

    Messages:
    600
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    88
    Digital Goods:
    10
    #4
    Parse error: syntax error, unexpected ',' I don't see where that is, any see it?
     
    sitescripts, Nov 14, 2012 IP
  5. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #5
    I don't do all the work free Eric. People don't learn if you hand them the answers. Then they flood forums with questions because they don't learn.
     
    DomainerHelper, Nov 14, 2012 IP
  6. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #6
    I know DomainerHelper; but i love to help for free! :)

    Error line i think is this one

    preg_match('/' . $search . '/'), $file)

    change it to

    preg_match('/' . $search . '/', $file)
     
    EricBruggema, Nov 14, 2012 IP
  7. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #7
    @stanfidavid; i don't mind, most bigger project goes to ppl who know the language! :)
     
    EricBruggema, Nov 15, 2012 IP
    ROOFIS likes this.