CKTIS / PHP
substr()
Extract a byte slice. Negative offset counts from the end and negative length excludes bytes from the far end.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
substr(string $string, int $offset, ?int $length = null): stringExtract a byte slice. Negative offset counts from the end and negative length excludes bytes from the far end. Omitting length or passing null takes the remaining bytes. An offset past the end returns an empty string, while a start before the beginning is clamped to zero. A cut may bisect a UTF-8 character.
Signature
substr(string $string, int $offset, ?int $length = null): string
Example output
["CD","BCD","","AB","c3"]
Examples
<?php
echo json_encode([substr("ABCDE",-3,2),substr("ABCDE",1,-1),substr("ABCDE",9),substr("ABC",-9,2),bin2hex(substr("\u{00E9}",0,1))]);
Result
["CD","BCD","","AB","c3"]