Created
December 15, 2020 08:59
-
-
Save fredbradley/0c78d2468066c3bc49bcb03ac2308ec1 to your computer and use it in GitHub Desktop.
FRB's De S'er
This file contains 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
/** | |
* We were auditing ~1000 iPads, and found that if you use a Barcode Scanner to | |
* get the Serial number of a product, it tended to prefix the serial number | |
* with "S". This was annoying as we were then trying to programatically | |
* match Serial Numbers against a 3rd party API which didn't have the | |
* S prefixed!. | |
* | |
* This gets rid of the S. We found that all our iPad serial numbers | |
* started with either "DM" or "F9", but you can change that by | |
* setting your own $startingChars. | |
*/ | |
if (! function_exists('deSSerialNumberFromFirstThreeCharacters')) { | |
function deSSerialNumberFromFirstThreeCharacters(string $input, array $startingChars=null): string | |
{ | |
// Firstly, all Serial Numbers should be upper case anyway. | |
// This catches any that have slipped through the net. | |
$input = strtoupper($input); | |
// If the user hasn't defined their own starting characters, we have our defaults here. | |
if (is_null($startingChars)) { | |
$startingChars = [ | |
"SDM", | |
"SF9" | |
]; | |
} | |
// Get the first three characters of our $input (serial number) | |
$substr = substr($input, 0, 3); | |
// Does the serial number start with one of our $startingChars? | |
if (in_array($substr, $startingChars)) { | |
// If so, then trim the (any amount) S off the left hand side of the string. | |
return ltrim($input, 'S'); | |
} | |
// If the serial number doesn't start with one of our startingChars, then just return string unchanged. | |
return $input; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment