开发者

Populating Database from Directory

开发者 https://www.devze.com 2023-02-19 07:58 出处:网络
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 expre

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消