Skip to content

Instantly share code, notes, and snippets.

@fredbradley
Created December 15, 2020 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredbradley/0c78d2468066c3bc49bcb03ac2308ec1 to your computer and use it in GitHub Desktop.
Save fredbradley/0c78d2468066c3bc49bcb03ac2308ec1 to your computer and use it in GitHub Desktop.
FRB's De S'er
/**
* 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