2

I want string format with incremental numbers. I've strings starting with alphabets and containing numbers with few leading 0's.

$string = M001;  //input
$alf= trim(str_replace(range(0,9),'',$string)); //removes number from string 
$number = preg_replace('/[A-Za-z]+/', '', $string);// removes alphabets from the string 
$number+=1;
$custom_inv_id = $alf.$number;

Expected result:

input M002 output M003
input A00003 output A00004

Using above code if input is M002, I'm getting output as M3. How I can get M003? Number of 0's is not fixed.

6
  • $custom_inv_id = $alf.sprintf('%03d', $number);.... you'll have to work out how many 0s you want from the length of $string - the length of $alf Commented Nov 26, 2015 at 12:41
  • do you want M009 to be M0010 or M010?? Commented Nov 26, 2015 at 12:43
  • @Surace: it should be M0010. Commented Nov 26, 2015 at 12:44
  • Though what do you want to happen when you get to 'M999' and try to increment? Commented Nov 26, 2015 at 12:44
  • @MarkBaker: leading 0's in number should be copied as it is. after that numbers should be incremental. Commented Nov 26, 2015 at 12:46

6 Answers 6

1

Use PHP preg_match or str_replace and try this code :-

$str='M001';   
preg_match('!\d+!', $str, $matches);
$num=(int)$matches[0];
$num++;

echo str_replace((int)$matches[0],'',$str);
echo $num;

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

@MaxZuber - As OP's question talked about strings with characters followed by numbers, and 'B2BA5' does not follow that pattern, why should the solution make allowance for it?
1
<?php

$tests = ['M0', 'M1', 'M001', 'M9', 'M09', 'M010',
  'M2M0', 'M2M1', 'M2M001', 'M2M9', 'M2M09', 'M2M010',
  'M2M', 'MM', '9M'];

foreach ($tests as $string) {
  if (preg_match('/([\w\W]+)([0-9]+)$/', $string, $matches)) {
    $output_string = $matches[1] . ($matches[2] + 1);
    echo '<p>' . $string . ' => ' . $output_string . '</p>';
  } else {
    echo '<p>' . $string . ' (nothing to increment)</p>';
  }
}

2 Comments

If you want to preserve all leading zeros, there is a simple solution I've just updated
Update it again with a full pack of possible tests.
0
$a = 'm002';
$pattern = '#(?P<word>[A-Za-z0]+)(?P<digtt>[1-9]+)#';
preg_match($pattern, $a, $matches);
$final = $matches['word'].(string)($matches['digtt']+1);
echo $final;

2 Comments

Can you please explain your solution, so it becomes more useful for others?
Please read stackoverflow.com/help/how-to-answer - "Brevity is acceptable, but fuller explanations are better."
0

You can use the sprintf and preg_match functions to get your expected result.

  • First: Split your string with preg_match to seperated values to work with
  • Second: Format a new string with sprintf

http://php.net/manual/en/function.sprintf.php http://php.net/manual/en/function.preg-match.php

function increase($string, $amount = 1) {
    $valid = preg_match("#^(.)(0+)?(\d+)$#s", $string, $matches);

    if($valid) {
        list($match, $char, $zero, $integer) = $matches;
        $integer += $amount;

        return sprintf("%s%'.0" . (strlen($zero)+1) . "d", $char, $integer);
     }

    return null;
}


echo increase("M00001"); // M00002
echo increase("A001", 5); // A006

2 Comments

you need to adjust the .03 string or make it dynamic or did i miss that he want one function for every case`?
@ins0: what should I use instead of .03 if the string is dynamic as Surace mentioned?
0

i hope this can help you . i have made some thing dynamic make M% also dynamic

<?php
$vl = "A00004";
$number =  substr($vl ,1);
$num= substr_count($vl,0);
$num++;
$number++;
$string = "M%'. 0".$num."d";

echo sprintf($string, $number); 

?>

i got this result

M00005

Comments

0

As leading 0 is copied, I would do it like this. It works if the leading chars is also lowercase. It's also a non-regex and non-array way.

$str = "M0099";
$num = ltrim($str, "a..zA..Z0");
$new = str_replace($num, $num + 1, $str);

Output:

echo $new; // string(6) "M00100"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.