How to Use substr() in PHP to Extract Substrings
Working with strings is a key part of PHP programming, and substr()
is one of the most useful functions for this. It extracts a portion of a string based on a start position and optional length.
What is substr()
?
substr()
extracts part of a string. It takes:
- $string: The original string.
- $start: The starting index (0-based). Negative values start from the end.
- $length (optional): Number of characters to extract. Negative values exclude chars from the end.
Syntax
substr(string $string, int $start, ?int $length = null): string
Examples
- Basic extraction
echo substr("Hello World", 6); // Output: World
Starts at index 6, returns the rest.
- Using length
echo substr("Hello World", 6, 3); // Output: Wor
Extracts 3 characters starting at index 6.
- Negative start
echo substr("Hello World", -5); // Output: World
Starts 5 characters from the end.
- Negative length
echo substr("Hello World", 0, -6); // Output: Hello
Returns string excluding last 6 characters.
When to Use substr()
?
- Text processing: Extract file extensions, manipulate URLs, or handle user input.
- Data cleanup: Trim unwanted parts.
- Custom formatting: Show previews or excerpts.
Final thoughts
substr()
is a flexible, essential PHP function for string manipulation. Get comfortable with its parameters to boost your PHP skills.
Quiz
- What is the output of:
echo substr("Programming in PHP", 0, 11);
- A) Programming
- B) in PHP
- C) Programmin
- D) PHP
- Which is true about
$start
insubstr()
?
- A) Must be positive
- B) Can be negative to start from the end
- C) Determines length
- D) Is optional
Drop your answers anytime!