Search code examples
phppreg-matchstring-formatting

Custom date and number format string (with padding and offsets)


I tried to create a customizable number according to a defined mask.
This is my rules to make a mask

You may enter any numbering mask. In this mask, the following tags could be used: {000000} corresponds to a number which will be incremented on each customer.

Enter as many zeros as the desired length of the counter.

The counter will be completed by zeros from the left in order to have as many zeros as the mask.

{000000+000} same as previous but an offset corresponding to the number to the right of the + sign is applied starting on first .

{000000@x} same as previous but the counter is reset to zero when month x is reached (x between 1 and 12).

If this option is used and x is 2 or higher, then sequence {yy}{mm} or {yyyy}{mm} is also required.

{dd} day (01 to 31).

{mm} month (01 to 12).

{yy}, {yyyy} or {y} year over 2, 4 or 1 numbers.

All other characters in the mask will remain intact.

Spaces are not allowed.

Example on customer created on 2007-03-01:
ABC{yy}{mm}-{000000} will give ABC0701-000099,
{0000+100}-ZZZ/{dd}/XXX will give 0199-ZZZ/31/XXX

So my current mask is C{000000}

 <?php
    $mask = "C{000000}";
$number = 100;
    if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i',$mask,$regType)){
        $masktype=$regType[1];
        $masktype_value=substr(preg_replace('/^TE_/','',$number),0,strlen($regType[1]));//get n first characters of code where n is length in mask
        $masktype_value=str_pad($masktype_value,strlen($regType[1]),"#",STR_PAD_RIGHT);
        $maskwithonlyymcode=$mask;
        $maskwithonlyymcode=preg_replace('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i',$regType[1],$maskwithonlyymcode);
        $maskwithonlyymcode=preg_replace('/\{dd\}/i','dd',$maskwithonlyymcode);
        $maskwithonlyymcode=preg_replace('/\{(c+)(0*)\}/i',$maskrefclient,$maskwithonlyymcode);
        $maskwithonlyymcode=preg_replace('/\{(t+)\}/i',$masktype_value,$maskwithonlyymcode);
        $maskwithnocode=$maskwithonlyymcode;
        $maskwithnocode=preg_replace('/\{yyyy\}/i','yyyy',$maskwithnocode);
        $maskwithnocode=preg_replace('/\{yy\}/i','yy',$maskwithnocode);
        $maskwithnocode=preg_replace('/\{y\}/i','y',$maskwithnocode);
        $maskwithnocode=preg_replace('/\{mm\}/i','mm',$maskwithnocode);
        print "maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";

    }

    ?>

But it is not working it is printing

maskwithonlyymcode=C000000 maskwithnocode=C000000

My desired output is C000001 - C000100.

What is missing in this code?


Solution

  • I do not understand your code much, so I was not able to fix it, but what about:

    <?
    
    function process_mask($mask, $number, $date)
    {
        while (preg_match("/\{(.+?)\}/", $mask, $match))
        {
            $outter_code = $match[0];
            $inner_code = $match[1];
            if (preg_match("/^(0+)(\+(\d+))?$/", $inner_code, $match2))
            {
                $number2 = $number;
                if (!empty($match2[3]))
                {
                    $number2 += intval($match2[3]);
                }
    
                $replacement = str_pad($number2, strlen($match2[1]), "0", STR_PAD_LEFT);
            }
            else
            {
                switch ($inner_code)
                {
                    case "dd":
                        $replacement = date("d", $date);
                        break;
                    case "mm":
                        $replacement = date("m", $date);
                        break;
                    case "y":
                        $replacement = substr(date("Y", $date), 3);
                        break;
                    case "yy":
                        $replacement = date("y", $date);
                        break;
                    case "yyyy":
                        $replacement = date("Y", $date);
                        break;
                    default:
                        trigger_error("Unrecognised code $inner_code");
                        return NULL;
                }
            }
    
            $mask = str_replace($outter_code, $replacement, $mask);
        }
    
        return $mask;
    }
    
    function test_mask($mask)
    {
        $date = mktime(0, 0, 0, 4, 19, 2013);
        echo str_pad($mask, 25)." => ".process_mask($mask, 100, $date)."\n";
    }
    
    test_mask("C{000}");
    test_mask("C{000000}");
    test_mask("C{000000+10}");
    test_mask("ABC{yy}{mm}-{000000}");
    test_mask("{0000+100}-ZZZ/{dd}/XXX");
    
    ?>
    

    Outputs:

    C{000}                    => C100
    C{000000}                 => C000100
    C{000000+10}              => C000110
    ABC{yy}{mm}-{000000}      => ABC1304-000100
    {0000+100}-ZZZ/{dd}/XXX   => 0200-ZZZ/19/XXX
    

    I absolutely do not undertand your rules about resetting counters. Based on what date do you want to reset the numbers? Current date? Do you keep some counter per customer (you have not explained what the number is)? Why resetting it on certain month? Wouldn't it be more meaningful to reset it in intervals? Like every month (implementation-wise, it would make sense then to keep separate counter for every month, so that the formatting logic is current time-independent). Some example may help understanding this.

    Also for date formatting, I would suggest you to stick with PHP date formatting and do not invent your own.

    I would suggest you to use pattern like this instead (It's actually bit .NET-like):

    {(#[+offset]|php-date-format-string)[:length]}
    

    So (for number = 999 and date = 2013-04-19):

    C{#:4}        => C0999
    C{#+10:4}     => C1009
    C{#:6}        => C000999
    C{#:4}/{Y}    => C0999/2013
    C{#:4}/{Y:4}  => C0999/2013
    C{#:4}/{Y:2}  => C0999/13
    C{#:4}/{Y:1}  => C0999/3
    C{#:4}/{m}    => C0999/03
    C{#:4}/{Y}{m} => C0999/201303
    C{#:4}/{Ym}   => C0999/201303
    

    Code for this would be way simpler, more extensible and flexible.