0

所以我正在尝试使用以下函数剖析我的列名

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'没有生成正确的StringLengthmovingAverageLoc(它显示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
4

2 回答 2

1
list($periodDuration, $periodLength, $movingAverage, $movingAverageLoc) =
    sscanf($columnName, '%d%[a-z]%d%[a-z]');
于 2013-04-22T21:06:23.697 回答
1

在这一行:

$movingAverageLoc = $movingAverages[array_search($columnName, $movingAverages)];

这部分:

array_search($columnName, $movingAverages)

正在搜索数组,array("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
)
于 2013-04-22T21:17:10.843 回答