1

我正在尝试提取字符串的位。

字符串如下所示:

$rowstr = "8:12-bk-16430|8:2012-bk-16430|1080751|7|||||10/30/2012|1/30/2012|||bk|||PINELLAS-FL|Tampa|Paid...

我使用这个得到第一个字段就好了:

$pos1 = strpos($rowstr, "|") +1; //begining of field case number
$pos2 = strpos($rowstr, "|", $pos1 + 1); //end of field case number
$len1 = $pos2 - $pos1;  //string length
$field['case_num'][$i] = substr($rowstr,$pos1,$len1); // casenumber extracted

但是当我尝试提取第二个字段时(应该是1080751

$pos3 = strpos($rowstr, "|", $pos2); //end of field 
$pos4 = strpos($rowstr, "|", $pos3 + 1); //end of field
$len2 = $pos3 - $pos4;  //string length
$field['field2'][$i] = substr($rowstr,$pos3,$len2); // field 2 extracted

结果是一个以 开头的很长的字符串|1080751|7|||||10/30/2012|10..

如果$pos3返回16$pos4返回,24那么为什么我得到的结果比 8 个字符长得多?PHP 是否出于某种原因不计算特殊包机?我怎样才能解决这个问题?

4

1 回答 1

1

尝试

// Added + 1 here
$pos3 = strpos($rowstr, "|", $pos2) + 1; //end of field
$pos4 = strpos($rowstr, "|", $pos3 + 1); //end of field
// swapped $pos3 and $pos4 here
$len2 = $pos4 - $pos3;  //string length
$field['field2'][$i] = substr($rowstr,$pos3,$len2); // field 2 extracted
于 2012-10-31T06:08:34.947 回答