如何通过php文件将信息从文件(如下所示)导入我的mysql数据库?
我想通过打开一个文件,在一个varbele中插入一行并检查每一行是否为=标记,并通过检查标签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=
答案 0 :(得分: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
数组
答案 1 :(得分: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*/