doushi1900 2016-11-10 14:52
浏览 33
已采纳

我如何在php中生成像DEL-1234这样的代码

Hey guys please i am new to php mysql, i am trying to generate a code like this DEL-12345. So if the person chooses the state Delaware in the form, it generates and stores DEL-23413 in the db on submit. DEL (First 3 letters of the state); - (Hyphen) and then 12345 (5 random numbers)...

please help

  • 写回答

2条回答 默认 最新

  • doudang4857 2016-11-10 15:02
    关注

    I would like to point you in the right direction with the code below.

    $Area = "Utah";
    $Code = strtoupper(substr($Area, 0, 3)) . "-" . (mt_rand(1000,9999));
    echo ( $Code );
    

    However, note that this code is subject to errors for example: What if the $Area variable contained a string less than 3 characters? What if the random value generated is not unique (the theory being you want it to be)

    All of this needs to be checked, by you, give it a go - we can help if needed but we wont write the whole code for you.

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?