所以我试图使用以下函数来剖析我的列名
function disectColumnName($columnName){
echo "fieldname: " .$columnName. "<br>";
$periodLengths = array("min","hr","dy","wk"); # Array of periodLength Options
$periodLength = $periodLengths[array_search($columnName, $periodLengths)]; # Find which periodLength is used in columnName and sa
$periodDuration = substr($columnName, 0, strpos($columnName,$periodLength));
echo 'periodLength: '.$periodLength . "<br>";
echo 'periodDuration: '.$periodDuration . "<br>";
$movingAverages = array("e","s");
$movingAverageLoc = $movingAverages[array_search($columnName, $movingAverages)];
echo "movingAverageLoc: " .$movingAverageLoc . "<br>";
$startOfString = strpos($columnName,$periodLength)+strlen($periodLength);
$stringLength = strpos($columnName,$movingAverageLoc)-(strpos($columnName,$periodLength)+strlen($periodLength));
# strpos($columnName,$movingAverageLoc) returns blank, should return strpos when movingAverages = "S"
echo "StartOf: ". $startOfString . "<br>";
echo "StrLen: ". $stringLength . "<br>";
$movingAverage = substr($columnName,$startOfString,$stringLength);
echo 'movingAverage: '.$movingAverage;
}
disectColumnName
处于循环中,通过以下列标题进行处理 -
array(12) { [0]=> string(7) "14min3e" [1]=> string(7) "14min4e" [2]=> string(8) "14min41e" [3]=> string(7) "14min3s" [4]=> string(8) "14min41s" [5]=> string(8) "14min32s" [6]=> string(9) "14min321s" [7]=> string(9) "15min321s" [8]=> string(8) "15min41s" [9]=> string(9) "15min321e" [10]=> string(8) "15min41e" [11]=> string(8) "15min32e" }
问题是以's'
结尾的列标题没有生成正确的StringLength
或movingAverageLoc
(它显示的是e
而不是S
)。但是,如果列名称以'e'
结尾,则它可以完美地运行,是什么?
以下是目前正在输出的内容:
fieldname: 14min3e
periodLength: min
periodDuration: 14
movingAverageLoc: e
StartOf: 5
StrLen: 1
movingAverage: 3
fieldname: 14min4e
periodLength: min
periodDuration: 14
movingAverageLoc: e
StartOf: 5
StrLen: 1
movingAverage: 4
fieldname: 14min41e
periodLength: min
periodDuration: 14
movingAverageLoc: e
StartOf: 5
StrLen: 2
movingAverage: 41
fieldname: 14min3s
periodLength: min
periodDuration: 14
movingAverageLoc: e
StartOf: 5
StrLen: -5
movingAverage:
fieldname: 14min41s
periodLength: min
periodDuration: 14
movingAverageLoc: e
StartOf: 5
StrLen: -5
movingAverage:
fieldname: 14min32s
periodLength: min
periodDuration: 14
movingAverageLoc: e
StartOf: 5
StrLen: -5
movingAverage:
fieldname: 14min321s
periodLength: min
periodDuration: 14
movingAverageLoc: e
StartOf: 5
StrLen: -5
movingAverage:
fieldname: 15min321s
periodLength: min
periodDuration: 15
movingAverageLoc: e
StartOf: 5
StrLen: -5
movingAverage:
fieldname: 15min41s
periodLength: min
periodDuration: 15
movingAverageLoc: e
StartOf: 5
StrLen: -5
movingAverage:
fieldname: 15min321e
periodLength: min
periodDuration: 15
movingAverageLoc: e
StartOf: 5
StrLen: 3
movingAverage: 321
fieldname: 15min41e
periodLength: min
periodDuration: 15
movingAverageLoc: e
StartOf: 5
StrLen: 2
movingAverage: 41
fieldname: 15min32e
periodLength: min
periodDuration: 15
movingAverageLoc: e
StartOf: 5
StrLen: 2
movingAverage: 32
答案 0 :(得分:1)
list($periodDuration, $periodLength, $movingAverage, $movingAverageLoc) =
sscanf($columnName, '%d%[a-z]%d%[a-z]');
答案 1 :(得分:1)
在这一行:
$movingAverageLoc = $movingAverages[array_search($columnName, $movingAverages)];
这部分:
array_search($columnName, $movingAverages)
正在搜索数组,数组(“e”,“s”)作为列名,例如“15min41e”。除非你的列名是“e”或“s”,否则它总是会返回false。因此,对于您的所有示例数据,它基本上都是这样做的:
$movingAverageLoc = $movingAverages[false];
与:
相同$movingAverageLoc = $movingAverages[0];
这就是为什么你总是得到$ movingAverageLoc的“e”。你至少需要修理那条线。
然而,有更容易(不那么令人困惑)的方式来做你想做的事情。使用preg_split获取一个:
$string = "1wk2dy5hr15min431e";
$res = preg_split('~([0-9]+)([a-z]+)~i', $string, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
print_r($res);
结果:
Array
(
[0] => 1
[1] => wk
[2] => 2
[3] => dy
[4] => 5
[5] => hr
[6] => 15
[7] => min
[8] => 431
[9] => e
)