Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

substr()

substr() is a PHP native function that extracts a portion of a string, starting at a given position, for an optional given length.

A negative start counts from the end of the string, rather than the beginning. A negative length stops the extraction that many characters before the end of the string, rather than counting a number of characters to keep.

When start is beyond the length of the string, or length resolves to zero or a negative amount that consumes the whole remaining string, substr() returns an empty string, since PHP 8.0; before, it could return false.

substr() is byte-oriented: it counts bytes, not characters, which corrupts multi-byte strings such as UTF-8. mb_substr() should be used instead, whenever the string may contain non-ASCII characters.

<?php

    substr('Hello World', 6);      // 'World'
    substr('Hello World', 0, 5);   // 'Hello'
    substr('Hello World', -5);     // 'World'
    substr('Hello World', 0, -6);  // 'Hello'
    substr('Hello', 10);           // '' (PHP 8+; was false before)

    // byte-oriented: mangles multi-byte characters
    substr('héllo', 0, 2);     // 'h' followed by half of the 'é' byte sequence
    mb_substr('héllo', 0, 2);  // 'hé'

?>

Documentation

See Also