2011-03-24 50 views
1

我想掃描一個具有圖像文件的目錄,並用它們填充我的數據庫。這些圖像具有常規和受控名稱,如top_1,top_200,bottom_3。我需要與正則表達式相匹配的正則表達式,因爲它們中的每一個在數據庫中都有不同的關係。從目錄填充數據庫

我有什麼權利現在掃描目錄:

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; 
       } 
      } 


    } 

與代碼拉弦任何幫助之前,「_」將是巨大的。任何幫助改善邏輯或代碼否則將錦上添花!提前致謝。

+1

$ A =爆炸( '_',$文件名); – Teson 2011-03-24 20:06:06

+0

這工作完美。謝謝。我接受了接受的答案,因爲它更簡單,並沒有爆炸成一個數組。 – Rapture 2011-03-24 20:16:47

回答

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

比爆炸更容易。謝謝。 – Rapture 2011-03-24 20:19:16

1
$str = 'asd_asdad_aef'; 

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

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

感謝您的回覆。我用爆炸結束了,結果我不需要正則表達式的複雜性。感謝你的回答。投了票。 – Rapture 2011-03-24 20:17:30

+0

謝謝。是的,爆炸效率更高。 – 2011-03-24 20:19:49