If you just want to ensure the size you create and use a function that does this whenever you want to ensure the size:
function fixedSTring($txt, $length) {
$length = max($length, strlen($length);
return str_pad(substr($txt, 0, $length), $length);
}
If you want something guaranteed and automatic, just create a new type, a new class that in addition to saving a string , save the fixed size of it and apply the above criteria. Something like that (not complete and tested, it's just a base)
final class FixedString {
private $txt = '';
public function __construct($txt, $length) {
$length = max($length, strlen($length));
$this->txt = str_pad(substr($txt, 0, $length), $length);
}
public function __toString() {
return $this->txt;
}
}
Usage:
$texto = new FixedString("teste", 20);
Obviously you need to do better checks, have more methods that will help the job. Making a new type should be very well thought out. I would not do the filling and cutting of the string automatically so, there are better ways to handle this, but the question does not go into detail.