简体   繁体   中英

Populating Database from Directory

I would like to scan a directory that has image files and populate my database with them. The images have regular and controlled names like top_1, top_200, bottom_3. I need help with the regular expression that will match the word before the '_' because each of these has a different relationship in the db.

What I have right now to scan the directory:

function scan_img_dir()
    {

            $dir = '/images/';  
            $scan = scandir($dir);  

            for ($i=0; $i<count($scan); $i++) 
            {  
                //Being Pseudocode
                $stringBeforeUnderscore = Some_String_Manipulation($scan[$i]);
                switch($stringBeforeUnderscore)
                {
                    case 'top':
                        insert into db with the top relationship
                        break;
                    case 'bottom':
                        insert into db with the bottom relationship
                        break;
                }
            }  


    }

Any help with the code to pull the string before the '_' would be great. Any help to improve the logic or the code otherwise would be icing on the cake! Thanks in advance.

$stringBeforeUnderscore = substr($scan[$i], 0, strpos($scan[$i], '_'));
$str = 'asd_asdad_aef';

preg_match_all('/([^_]*)/', $str, $matches);

$stringBeforeUnderscore = $matches[0][0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM