echo substr('Hello world!', 6, 5);
// Extracts 5 characters starting at position 6 (zero-based index)
// Output: world
substr(string $string, int $start, ?int $length = null): string
$originalString = 'Hello, world!';
$start = 7;           // Start position for extraction
$length = 5;          // Number of characters to extract

$extractedString = substr($originalString, $start, $length);

if ($extractedString !== false && $extractedString !== '') {
    echo 'Extracted string: ' . $extractedString;
} else {
    echo 'Failed to extract substring';
}
// Output: Extracted string: world
$originalString = 'Hello, world!';
$startNegative = -6;    // Start from the 6th character from the end
$length = 5;            // Number of characters to extract

$extractedString = substr($originalString, $startNegative, $length);

echo 'Extracted string: ' . $extractedString;
// Output: Extracted string: world
// Example 1:
// Extract from index 0 up to -1 (i.e., one character before the end).
// -1 refers to the last character, so this omits the final character.
echo substr('Hello world', 0, -1);
// Output: Hello worl

// Example 2:
// Start from -9 and extract up to -3.
// -9 starts 9 characters from the end, and -3 ends 3 characters from the end.
// Resulting substring: 'llo wo'
echo substr('Hello world', -9, -3);
// Output: llo wo

// Example 3:
// Start from index 0 and extract up to -4.
// -4 refers to the fourth character from the end (i.e., index 7).
// Resulting substring: 'Hello w'
echo substr('Hello world', 0, -4);
// Output: Hello w
echo substr('こんにちは世界', 0, 5);
// Output might be garbled due to cutting within multibyte characters
Output
echo mb_substr('こんにちは世界', 0, 5);
Output