开发者

How can I obtain the ID from the following example string?

I use the following method to pad out a ID for a property on our website:

function generateAgentRef($id,$length=5,$strPrefix='1'){
    return $strPrefix . str_pad($id,$length,0,0);
}

Basically it will prefix 1 and then pad out the id with 0's until the string reaches $length.

But, I now have a requirement to revert this process. For example if I have the following IDs: 100650,100359,100651,100622,100112,100687, how can I get the ID e.g. 650, 359, 651, 622, 112, 687?

Hope this explains what I'm trying to achieve.

The ID in the database will never start with 0, so I was thinking of iterating over the components of the string and detecting wh开发者_如何转开发en I hit something other than 0 and then splitting the string.


substract 100000 from the generated ref and intval() it could work if the length is 6 numbers exactly.


try using this $a = substr($num,3);

here $num is the id you get $a will be your desired number i.e 100659 shortened to 659


Expanding on your initial function

function getAgentId($id, $length = 5, $strPrefix = 1){
    return $id - generateAgentRef(0, $length, $strPrefix);
}

$id = generateAgentRef(255);
echo $id, PHP_EOL; // 100255
echo getAgentId($id), PHP_EOL; //255
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜