简体   繁体   中英

Issue with strpos, strlen, and substr

So I am trying to dissect my column names using the following function

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 is in a loop which process through the following array of column titles -

 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" }

The problem is that the column titles ending in 's' are not generating the correct StringLength or movingAverageLoc (it's showing e instead of S ). However if the column name ends in 'e' , it works perfectly, what gives?

Here is what is currently being output:

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
list($periodDuration, $periodLength, $movingAverage, $movingAverageLoc) =
    sscanf($columnName, '%d%[a-z]%d%[a-z]');

In this line:

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

This part:

array_search($columnName, $movingAverages)

is searching the array, array("e","s") for the column name, "15min41e" for example. Unless your column name is "e" or "s", it's always going to return false. So for all your example data it's essentially doing this:

$movingAverageLoc = $movingAverages[false];

which is the same as:

$movingAverageLoc = $movingAverages[0];

That's why you're always getting "e" for $movingAverageLoc. You'll need to fix that line at least.

However, There are much easier (less confusing) ways of doing what you want to do. Using preg_split for one:

$string = "1wk2dy5hr15min431e";
$res = preg_split('~([0-9]+)([a-z]+)~i', $string, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
print_r($res);

results in:

Array
(
    [0] => 1
    [1] => wk
    [2] => 2
    [3] => dy
    [4] => 5
    [5] => hr
    [6] => 15
    [7] => min
    [8] => 431
    [9] => e
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM