PHP substr() Function
PHP substr() Syntax
substr(string $string, int $offset, ?int $length = null): string
PHP substr() Basic examples
echo substr("abcdef", 1);
bcdef
Returns the string starting from position 1.
echo substr("abcdef", 1, 3);
bcd
Returns a substring of length 3 starting from position 1.
echo substr("abcdef", 0, 4);
abcd
Returns the first 4 characters of the string.
PHP substr() Real-world usage
$filename = "report_2024.pdf";
$extension = substr($filename, -3);
echo $extension;
pdf
Extracts the last 3 characters of a string, commonly used for file extensions.
$text = "This is a long text";
if (strlen($text) > 10) {
echo substr($text, 0, 10) . "...";
}
This is a ...
Truncates a string to a specific length and appends ellipsis.
PHP substr() Edge cases
var_dump(substr("abc", 5));
bool(false) or string(0) "" depending on PHP version
If the start position is out of bounds, it returns false or an empty string.
PHP substr() Common mistakes
Incorrectly assuming the third parameter is the end position
The third parameter is length, not the end position.
// Trying to get from pos 1 to 3
echo substr("abcdef", 1, 3); // result is 'bcd', not 'bc'
echo substr("abcdef", 1, 2); // gets 'bc'
The third parameter is the length of the substring.
PHP substr() Frequently Asked Questions
What does substr() do in PHP?
substr() returns a portion of a string based on a starting position and optional length.
How does substr() work with negative start values?
If the start is negative, substr() begins that many characters from the end of the string.
What happens if the length parameter is omitted in substr()?
If length is not provided, substr() returns the substring from the start position to the end of the string.
Can substr() return an empty string?
Yes, substr() returns an empty string if the start position is beyond the string length or if length is zero.
What is the difference between substr() and mb_substr()?
substr() works with bytes, while mb_substr() supports multibyte encodings like UTF-8 and should be used for non-ASCII strings.
Is substr() safe for UTF-8 strings?
No, substr() is not multibyte-safe. Use mb_substr() for UTF-8 or multibyte strings.
Can substr() use negative length values?
Yes, a negative length omits that many characters from the end of the string.
What happens if the start position exceeds string length?
substr() returns an empty string if the start position is greater than the string length.
What is the difference between substr() and slice functions in other languages?
substr() behaves similarly to slicing in other languages but uses a start and optional length instead of start and end indexes.
How to extract the last N characters using substr()?
Use a negative start value, for example substr($string, -3) to get the last 3 characters.
Does substr() modify the original string?
No, substr() returns a new string and does not modify the original variable.
What is a real-world use case of substr()?
substr() is commonly used to extract parts of strings such as file extensions, substrings from user input, or formatted data segments.