Wikis & learning

Slice a predictable reference code

A fixed-format ASCII identifier can be split by position. Work out which characters represent the series and which represent the issue before using substring offsets.

A fixed-format ASCII identifier can be split by position. Work out which characters represent the series and which represent the issue before using substring offsets.

Walkthrough

PHP starts counting at zero. A negative start counts backward from the end of a byte string. This approach works for the reference format in the example; slicing arbitrary human-language text needs character-aware functions.

PHP example

<?php
$reference = 'DOC-017';
$series = substr($reference, 0, 3);
$issue = substr($reference, -3);
echo $series . ' / ' . $issue;

Expected output

DOC / 017

Change and check

Extend the numeric part to four characters and adjust the suffix extraction. Check a malformed identifier separately before using this approach on incoming data.