0

I am trying to add the int with strings in PHP but I could get a helpful answer on StackOverflow and other website communities.

I want the code separate the ALPHABETS and NUMBER and then add the value in NUMBER and again JOIN the ALPHABETS AND ADDED NUMBER together.

How I achieve this, Please help.

Example:

$mynumber  = 'SBI0001';
$addValue = '1';

It will be 'SBI0002' in PHP.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Take a look at this post: https://stackoverflow.com/a/4538057/7993505 – Richard Apr 06 '21 at 06:41
  • Search for each part separately. Eg _"php extract prefixed number"_ and _"php format number with leading zeros"_ – Phil Apr 06 '21 at 06:42
  • @Richard it showing only number 1 but it should be `001` `preg_match("/(\\d+)([0-9]+)/", "MFG001", $matches); $string = $matches[1]; $number = $matches[2]; echo $number;` – Shubham Nagar Apr 06 '21 at 06:49
  • @ShubhamNagar You can not add an int (`1`) to an string (`001`): Workflow: 1) find your number | 2) count the zeros e.g.: `preg_match_all('/(0*)1/', $original_input, $output_array);` | 3) rebuild your string = `string`+`zeros`*x+`newValue` – Richard Apr 06 '21 at 08:30

0 Answers0