PHP substr() - Extract Substring
In this tutorial, you will learn how to use the PHP substr() function to extract parts of a string starting at a specified position. Whether you want to get a small segment or a large portion from a string, substr() is the go-to function in PHP for substring extraction.
Prerequisites
- Basic knowledge of PHP programming language
- PHP installed on your local machine or access to a PHP-enabled server
- Text editor or IDE to write PHP scripts
Setup Steps
-
Ensure PHP is installed:
php -vYou should see output indicating your PHP version.
-
Create a PHP file, e.g.,
substr-example.php. -
Open the file in your editor and start writing PHP code using the
substr()function. -
Run the script via command line or browser to see results.
Understanding PHP substr() Function
The substr() function extracts a portion of a string specified by a start position and optionally a length.
Function syntax:
substr(string $string, int $start, int|null $length = null): string|false
$string: The input string to extract from.$start: The starting position (0-based index). If negative, counts from the end.$length: (Optional) Number of characters to extract. If omitted, extracts to the end of the string. If negative, extracts up to that many characters from the end.
Examples with Explanation
Example 1: Extract substring from position
<?php
$text = "Hello, World!";
echo substr($text, 7); // Output: World!
?>
Explanation: Extracts from the 7th position (0-based), outputs "World!"
Example 2: Extract substring with length
<?php
$text = "Hello, World!";
echo substr($text, 0, 5); // Output: Hello
?>
Explanation: Extracts first 5 characters starting at position 0.
Example 3: Using negative start
<?php
$text = "Hello, World!";
echo substr($text, -6); // Output: World!
?>
Explanation: Starts 6 characters from the end, extracts till the end.
Example 4: Using negative length
<?php
$text = "Hello, World!";
echo substr($text, 0, -7); // Output: Hello,
?>
Explanation: Extracts from start (0) but stops 7 characters before the end.
Example 5: Extract substring with multibyte character strings
<?php
$text = "ΠΡΠΈΠ²Π΅Ρ, ΠΌΠΈΡ!"; // Russian for "Hello, world!"
echo substr($text, 0, 6); // Output could be broken characters
?>
Note: The substr() function is not multibyte-safe. For multibyte strings, use mb_substr().
Best Practices
- Always check if the string length and indexes are valid to prevent false or unexpected empty strings.
- Use negative start offsets to extract from the end of strings.
- For multibyte character strings (e.g., UTF-8), use
mb_substr()instead ofsubstr()to avoid broken characters. - Use
strlen()to determine string length when dynamically calculating positions. - When the length parameter is omitted, be mindful that extraction goes to the end of the string.
Common Mistakes
- Using
substr()on strings with multibyte characters withoutmb_substr()leading to broken output. - Passing a start index outside the bounds of the string, which returns
falseor empty string unexpectedly. - Confusing zero-based indexing with one-based indexing.
- Using negative length that is greater in absolute value than the string length, which returns an empty string.
- Not handling the possibility of
substr()returningfalseand assuming always string output.
Interview Questions
Junior Level
-
Q1: What does the PHP
substr()function do?
A1: It extracts a part of a string starting at a specified position, optionally for a specified length. -
Q2: How is the starting position counted in
substr()?
A2: It is zero-based, meaning position 0 is the first character. -
Q3: What happens if you omit the length parameter?
A3: Extraction continues from the start position to the end of the string. -
Q4: Can
substr()extract string portions counting from the end?
A4: Yes, by using a negative starting position. -
Q5: What value does
substr()return if starting position is out of range?
A5: It returns an empty string orfalse.
Mid Level
-
Q1: How does
substr()behave if the length is negative?
A1: It returns the substring starting at the start position and omits that many characters from the end. -
Q2: Explain why
substr()may cause issues with UTF-8 strings?
A2: Becausesubstr()works at the byte level and not character level, it may split multibyte chars causing broken output. -
Q3: How can you safely extract substrings from multibyte strings?
A3: Use themb_substr()function designed for multibyte-safe substring extraction. -
Q4: What is the impact of zero length parameter in
substr()?
A4: A length of zero returns an empty string immediately. -
Q5: How would you extract the last 4 characters of a string "abcdef" using
substr()?
A5: Usesubstr("abcdef", -4), which outputs "cdef".
Senior Level
-
Q1: How can improper use of
substr()impact security or functionality in real applications?
A1: Incorrect substring extraction may cause truncation of sensitive data or improper display leading to logic errors or information leakage. -
Q2: Discuss the performance implications of using
substr()on very large strings.
A2:substr()is efficient as it doesnβt copy the entire string, but improper repeated calls may cause overhead; consider alternatives if used heavily. -
Q3: How would you write a multibyte-safe wrapper function for
substr()that defaults to UTF-8?
A3: Usemb_substr()internally, with parameters for encoding defaulting to "UTF-8" and fallback tosubstr()ifmbstringis unavailable. -
Q4: How can you validate inputs to
substr()to ensure no runtime warnings or errors?
A4: Check if start and length are integers, and within bounds based on string length; handle negative lengths carefully. -
Q5: Explain subtle differences between
substr()andmb_substr()in the context of string slicing.
A5:substr()counts bytes, may corrupt multibyte characters;mb_substr()counts characters respecting multibyte encoding, making it accurate for non-ASCII strings.
Frequently Asked Questions (FAQ)
Q1: Can I use substr() to extract substrings from an empty string?
A1: Yes, but it will simply return an empty string without error.
Q2: What will substr("Hello", 10) return?
A2: It returns an empty string because the start position is beyond the end of the string.
Q3: Is substr() case-sensitive?
A3: substr() does not modify case; it only extracts substrings as-is.
Q4: How to extract the middle word from "red blue green" using substr()?
A4: You can use the start and length parameters to extract "blue", for example: substr("red blue green", 4, 4).
Q5: What PHP version introduced substr()?
A5: substr() has been available since PHP 4.
Conclusion
The PHP substr() function is a fundamental tool for extracting parts of strings based on specific positions and lengths. Understanding how to correctly use start and length parameters, including negative indexes, is essential for manipulating strings effectively in PHP. Remember to switch to mb_substr() for multibyte character support. By following best practices and avoiding common mistakes, you will harness the full power of substr() in your PHP projects.