2014-07-15 88 views
0

我怎樣才能從一個文件(如下圖)通過php文件導入到我的mysql數據庫的信息?從php文件導入信息到mysql

我在想通過打開一個文件,插入一行到一個變量,並檢查每一行=標記,並通過檢查標記bevore = =來將信息插入到數組中。

這是正確的方式還是有更好的方法。

示例文件:

# Disc length: 926 seconds 
# Revision: 0 
# Processed by: cddbd v1.5PL3 Copyright (c) Steve Scherf et al. 
# Submitted via: audiograbber 1.83.01 

DISCID=00039c14 
DTITLE=3M Littmann Stethoscope Edition/20 Beispiele zur Herz- und Lungenausku 
DTITLE=ltation 
DYEAR=1997 
DGENRE=Medizin 
TTITLE0=Normale Herztöne 
TTITLE1=Dritter Herzton (physiologisch) 
TTITLE2=Vierter Herzton 
TTITLE3=Aortenklappenstenose 
TTITLE4=Mitralklappeninsuffizienz 
TTITLE5=Mittelsystolischer Klick 
TTITLE6=Ventrikelseptum-Defekt 
TTITLE7=Atriumseptum-Defekt 
TTITLE8=Mitralklappenstenose 
TTITLE9=Aortenklappeninsuffizienz 
TTITLE10=normales tracheales Atemgeräusch 
TTITLE11=normales vesikuläres Atemgeräusch 
TTITLE12=feine Krepitation mit leichter bronchialer Atmung 
TTITLE13=rauhe Krepitation 
TTITLE14=bronchiale Atmung 
TTITLE15=Stridor beim Einatmen 
TTITLE16=Rhonchus 
TTITLE17=pfeifender Rhonchus (Keuchatmen) 
TTITLE18=feine Krepitation (Knisterrasseln) 
TTITLE19=pleurales Reibungsgeräusch 
EXTD= 
EXTT0= 
EXTT1= 
EXTT2= 
EXTT3= 
EXTT4= 
EXTT5= 
EXTT6= 
EXTT7= 
EXTT8= 
EXTT9= 
EXTT10= 
EXTT11= 
EXTT12= 
EXTT13= 
EXTT14= 
EXTT15= 
EXTT16= 
EXTT17= 
EXTT18= 
EXTT19= 
PLAYORDER= 
+1

正確的方法是使用http://php.net /manual/en/function.parse-ini-file.php – MightyPork

+0

顯示您迄今爲止編寫的代碼以獲取建議。你的解釋不清楚,所以很難說你的方法是否好。 –

+0

檢查此https://github.com/austinhyde/IniParser/blob/master/src/IniParser.php – Tushar

回答

0

試試這個:

$settingArray = array(); 
$text = file('file.txt'); 
foreach($text as $line){ 
    $line = str_replace(array("\r", "\n"), '', $line); 
    if(substr($line, 0, 1) != '#' && !empty($line)){ 
     $key = substr($line, 0, strpos($line, "=")); 
     $value = substr($line, strpos($line, "=") + 1); 
     $value = substr($value, 0, strpos($value, "#") != 0 ? strpos($value, "#") : strlen($value)); //removes everything behind a # sign 

     $settingArray[$key] = $value; 
    } 
} 

var_dump($settingArray); 
/*this will print: 
array (size=46) 
    'DISCID' => string '00039c14' (length=8) 
    'DTITLE' => string 'ltation' (length=7) 
    'DYEAR' => string '1997' (length=4) 
    'DGENRE' => string 'Medizin' (length=7) 
    'TTITLE0' => string 'Normale Herztöne ' (length=17) 
    'TTITLE1' => string 'Dritter Herzton (physiologisch)' (length=31) 
    'TTITLE2' => string 'Vierter Herzton' (length=15) 
    'TTITLE3' => string 'Aortenklappenstenose' (length=20) 
    'TTITLE4' => string 'Mitralklappeninsuffizienz' (length=25) 
    'TTITLE5' => string 'Mittelsystolischer Klick' (length=24) 
    'TTITLE6' => string 'Ventrikelseptum-Defekt' (length=22) 
    'TTITLE7' => string 'Atriumseptum-Defekt' (length=19) 
    'TTITLE8' => string 'Mitralklappenstenose' (length=20) 
    'TTITLE9' => string 'Aortenklappeninsuffizienz' (length=25) 
    'TTITLE10' => string 'normales tracheales Atemgeräusch' (length=32) 
    'TTITLE11' => string 'normales vesikuläres Atemgeräusch' (length=33) 
    'TTITLE12' => string 'feine Krepitation mit leichter bronchialer Atmung' (length=49) 
    'TTITLE13' => string 'rauhe Krepitation' (length=17) 
    'TTITLE14' => string 'bronchiale Atmung' (length=17) 
    'TTITLE15' => string 'Stridor beim Einatmen' (length=21) 
    'TTITLE16' => string 'Rhonchus' (length=8) 
    'TTITLE17' => string 'pfeifender Rhonchus (Keuchatmen)' (length=32) 
    'TTITLE18' => string 'feine Krepitation (Knisterrasseln)' (length=34) 
    'TTITLE19' => string 'pleurales Reibungsgeräusch' (length=26) 
    'EXTD' => boolean false 
    'EXTT0' => boolean false 
    'EXTT1' => boolean false 
    'EXTT2' => boolean false 
    'EXTT3' => boolean false 
    'EXTT4' => boolean false 
    'EXTT5' => boolean false 
    'EXTT6' => boolean false 
    'EXTT7' => boolean false 
    'EXTT8' => boolean false 
    'EXTT9' => boolean false 
    'EXTT10' => boolean false 
    'EXTT11' => boolean false 
    'EXTT12' => boolean false 
    'EXTT13' => boolean false 
    'EXTT14' => boolean false 
    'EXTT15' => boolean false 
    'EXTT16' => boolean false 
    'EXTT17' => boolean false 
    'EXTT18' => boolean false 
    'EXTT19' => boolean false 
    'PLAYORDER' => boolean false*/ 
+0

感謝您的輸入:-)。這真的幫助了我。 – user3837518

0

你應該簡單忽略#開頭的行,然後在所有其他線路(不空)找到=標誌第一次出現和使用susbtr(或mb_substr),你會得到列和列值。

編輯

有人認爲這是INI文件但事實上並非如此,你不能使用parse_ini_file功能。第一件事是,這裏註釋以「#'什麼會產生不推薦使用警告(INI PHP 5.5.12),此外,因爲沒有引號使用你會得到警告:

Warning: syntax error, unexpected '(' in test.ini on line 12 

這個函數將返回false

EDIT2

我會做這種方式:

<?php 

$data = array(); 
$lines = file('test.ini'); 

mb_internal_encoding('UTF-8'); 

foreach($lines as $line){ 
    $line = trim($line); 
    if ($line == '' || mb_substr($line,0,1) == '#') { 
     continue; 
    } 

    $pos = mb_strpos($line, '='); 

    if ($pos == false) { // == not === 
     continue;  
    } 


    $data[mb_substr($line,0,$pos)] = mb_substr($line, $pos + 1); 

} 

var_dump($data); 

如前所述,您需要先修整每一行並刪除從#開始的空行和行,然後檢查是否出現=。如果它沒有找到或放置在0簡單,你需要忽略這一行,其他所有線路都放置到$data陣列

+0

這是一個INI文件,所以手動解析它並不是最好的想法。 – MightyPork

+0

@MightyPork請看編輯的問題 –

+0

感謝downvoting球員。這個答案有什麼問題? –