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];
精彩评论