开发者

How can show my id field in 8 charater length string

In my listing page I have to show the record ID in eight character length string. This needs to be combination of letters and digits. The first 4 characters need to be letters and last 4 need to be digits. For viewing purpose alone I need to show the record ID in this formatted manner.

If a record ID has less than 4 digits, then add prefix 0's in front of them.

For example:

record #2 needs to show like AAAA0002.

record #10001 needs to be like AAAB0001.

The first part of 4 letters starts with A, if the record ID has more than 4 digits, the respected characters need to be incremented in the same manner开发者_开发技巧.

Can any one suggest me your valuable advice in this ?


Ok, I hope I understood it:

You have record IDs which can consist of max. 8 digits. If an ID has less than 8 digits, it should be prepended with '0'. You can do so by using sprintf[docs]:

// $id is 10001
$id = sprintf('%08d', $id); // 00010001

Then you want to convert the first four digits into letters, where (it seems) 0=>A, 1=>B,.... You can create such a mapping with range[docs]:

$char_map = range('A', 'J'); // array(0 => A, 1 =>B, ..., 9 => J);

To convert the digits, you can use strtr[docs], and to only get the first four digits, substr[docs]:

$id = strtr(substr($id, 0, 4), $char_map) . substr($id, 4); // AAAB0001

Here is a DEMO

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜