I have a problem which I have found a solution for, but I'd like to know why php is behaving this way.
I have a file format with this layout
{Key,dataLen}data{Key2,dataLen2}data2 ....
where dataLen can be any size but for this project it's always between 20 or 30 bytes.
and I did a function to parse it: You get the data from $processInfo , extract a {key,datalen}data info, and remove it from $processInfo string.
Code: Select all
function getKeyOldAndSlow ()
{
global $processInfo;
if ($processInfo[0]!='{') return FALSE;
$rv=array();
$index1=strpos ($processInfo,',');
$index2=strpos ($processInfo,'}');
$rv['key']=substr ($processInfo,1,$index1-1);
$rv['length']=substr ($processInfo,$index1+1,$index2-$index1-1);
$rv['value']=substr ($processInfo,$index2+1,$rv['length']);
$processInfo=substr ($processInfo,$index2+1+$rv['length']);
return $rv;
}
Code: Select all
function getKey ()
{
global $processInfo;
global $PIindex;
if ($processInfo[$PIindex]!='{') return FALSE;
$rv=array();
$index1=strpos ($processInfo,',',$PIindex);
$index2=strpos ($processInfo,'}',$PIindex);
$rv['key']=substr ($processInfo,$PIindex+1,$index1-$PIindex-1);
$len=$rv['length']=substr ($processInfo,$index1+1,$index2-$index1-1);
$rv['value']=substr ($processInfo,$index2+1,$len);
$PIindex=$index2+1+$len;
return $rv;
}
As a C programmer, I can't imagine why it's working this way
TIA
Kak