我有一个使用这种格式的cfg文件:
$array[user] = user2986
$array[pass] = pass2986
$array[startdate] = '';
$array[enddate] = '';
$array[info] = '';
$array[dealer] = '';
$array[user] = user3258
$array[pass] = pass3258
$array[startdate] = '2015-05-15';
$array[enddate] = '2015-11-17';
$array[info] = 'girlfriend data';
$array[dealer] = 'x-admin';
所以我只需要阅读数组:
Sub callfrm()
Dim wbfrm As Workbook
Set wbfrm = Workbooks.Open("C:\path\to\Book2.xlsm")
Application.Run "'"& wbfrm.Name & "'!showfrm"
set wbfrm = nothing
End Sub
如何在PHP中执行此操作?我尝试读取文件,但我不知道如何解析H:标签并将其放入while循环到数组中...任何示例代码?
由于
答案 0 :(得分:1)
您可以尝试此功能:
它适用于给定的数据,这是非常有限的。
function parseCfg($file) {
$cfg = array();
foreach (file($file) as $line) {
if (preg_match('/H: ([A-za-z0-9]+) ([A-za-z0-9]+)( # (\{.*?\})){0,1}/', $line, $match)) {
$data = array(
'user' => $match[1],
'pass' => $match[2],
'startdate' => '',
'enddate' => '',
'info' => '',
'dealer' => ''
);
if (isset($match[4])) {
$pretty = str_replace(array(' ', ' | '), array(' ', '&'), substr($match[4], 2, -2));
parse_str($pretty, $additional);
$data = array_merge($data, $additional);
}
$cfg[] = $data;
}
}
return $cfg;
}
答案 1 :(得分:0)
$str = 'H: user2986 pass2986
H: user3258 pass3258 # { startdate=2015-05-15 | enddate=2015-11-17 | info=girlfriend data | dealer=x-admin }
FILE CHANNELINFO: "/other/config.cfg"';
$expStr = explode("\n", $str);
for ($i=0;$i<count($expStr);$i++) {
$exp2Str = explode(" ", $expStr[$i]);
var_dump($exp2Str);
}
这是你的起点吗?
只需删除无用数据,然后将其余数据添加到循环中的数组中!
当然,只有在字符串始终相同时才有效!
输出:
array(3) {
[0]=>
string(2) "H:"
[1]=>
string(8) "user2986"
[2]=>
string(9) "pass2986
"
}
array(15) {
[0]=>
string(2) "H:"
[1]=>
string(8) "user3258"
[2]=>
string(8) "pass3258"
[3]=>
string(1) "#"
[4]=>
string(1) "{"
[5]=>
string(20) "startdate=2015-05-15"
[6]=>
string(1) "|"
[7]=>
string(18) "enddate=2015-11-17"
[8]=>
string(1) "|"
[9]=>
string(0) ""
[10]=>
string(15) "info=girlfriend"
[11]=>
string(4) "data"
[12]=>
string(1) "|"
[13]=>
string(14) "dealer=x-admin"
[14]=>
string(2) "}
"
}
array(1) {
[0]=>
string(1) "
"
}
array(3) {
[0]=>
string(4) "FILE"
[1]=>
string(12) "CHANNELINFO:"
[2]=>
string(19) ""/other/config.cfg""
}