Created
April 4, 2017 18:31
-
-
Save chrisblackwell/54295711d3df824553a5552f3c51a1b6 to your computer and use it in GitHub Desktop.
Calculate the next increment value of a string of numbers and letters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$testCases[] = '123'; // Should be 124 | |
$testCases[] = '123=RH'; // Should be 124=RH | |
$testCases[] = '123RH'; // Should be 124RH | |
$testCases[] = '123R4'; // Should be 124R4 | |
$testCases[] = 'RH123'; // Should be RH124 | |
$testCases[] = 'RH-123'; // Should be RH-124 | |
foreach($testCases as $testCase) { | |
$orderNumber = extractNumbersFromString($testCase); | |
$orderPrefix = extractOrderPrefix($testCase, $orderNumber); | |
print(incrementOrderNumber($orderNumber, $orderPrefix) . "\n"); | |
} | |
function extractNumbersFromString($string) { | |
preg_match_all('!\d+!', $string, $matches); | |
return $matches[0][0]; | |
} | |
function incrementOrderNumber($order_number, $prefix) { | |
$order_number = ( (int) $order_number ) + 1; | |
if($prefix[0] !== '') { | |
return $prefix[0] . $order_number; | |
} | |
if($prefix[1] !== '') { | |
return $order_number . $prefix[1]; | |
} | |
return (string) $order_number; | |
} | |
function extractOrderPrefix($string, $orderNumber) { | |
return explode($orderNumber, $string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment