In PHP, string manipulation is one of the most common tasks during development. Whether processing user input, generating dynamic content, or extracting useful information from lengthy text, you may need to extract substrings. PHP offers many powerful functions to efficiently handle strings, and substr is one of them.
substr is a built-in PHP function used to extract a specific portion from a string. It allows you to easily extract a substring starting from any position within the string.
Function Prototype:
<span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$string</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$start</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span>|</span><span><span class="hljs-literal">null</span></span><span> </span><span><span class="hljs-variable">$length</span></span><span> = </span><span><span class="hljs-literal">null</span></span><span>): </span><span><span class="hljs-keyword">string</span></span><span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
$string: The original string to extract from.
$start: The starting position. It can be negative, indicating counting from the end of the string.
$length (optional): The length of the substring to extract. If omitted, the substring will extend from the start position to the end of the string.
Suppose we have the string "Hello, world!". To extract the first 5 characters from the left, we can use the substr function. The code is as follows:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$string</span></span><span> = </span><span><span class="hljs-string">"Hello, world!"</span></span><span>;
</span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-variable">$string</span></span>, </span><span><span class="hljs-number">0</span></span><span>, </span><span><span class="hljs-number">5</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$result</span></span><span>; </span><span><span class="hljs-comment">// Output: Hello</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
In this example, 0 indicates the first character of the string, and 5 specifies that 5 characters should be extracted. The output is therefore "Hello".
You can also control the extraction length based on dynamic conditions. For example, if you want to extract only part of a string according to a condition, set $length to a variable value:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$string</span></span><span> = </span><span><span class="hljs-string">"Welcome to PHP world!"</span></span><span>;
</span><span><span class="hljs-variable">$length</span></span><span> = </span><span><span class="hljs-number">7</span></span><span>;
</span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-variable">$string</span></span>, </span><span><span class="hljs-number">0</span></span><span>, </span><span><span class="hljs-variable">$length</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$result</span></span><span>; </span><span><span class="hljs-comment">// Output: Welcome</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
The substr function supports negative indexes, meaning you can extract from the right end of a string. This is useful for operations that require counting from the end.
For example, to extract the last three characters of a string:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$string</span></span><span> = </span><span><span class="hljs-string">"Learning PHP is fun!"</span></span><span>;
</span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-variable">$string</span></span>, -</span><span><span class="hljs-number">3</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$result</span></span><span>; </span><span><span class="hljs-comment">// Output: fun</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
Here, -3 indicates starting from the third-to-last character to the end of the string.
A common scenario is extracting a file's extension. Suppose you have a file path and need to get its extension. You can achieve this by combining substr with strrpos.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$file_name</span></span><span> = </span><span><span class="hljs-string">"document.pdf"</span></span><span>;
</span><span><span class="hljs-variable">$dot_position</span></span><span> = </span><span><span class="hljs-title function_ invoke__">strrpos</span></span><span>(</span><span><span class="hljs-variable">$file_name</span></span>, </span><span><span class="hljs-string">'.'</span></span><span>); </span><span><span class="hljs-comment">// Find the position of the last dot</span></span><span>
</span><span><span class="hljs-variable">$extension</span></span><span> = </span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-variable">$file_name</span></span>, </span><span><span class="hljs-variable">$dot_position</span></span><span> + </span><span><span class="hljs-number">1</span></span><span>); </span><span><span class="hljs-comment">// Extract the part after the dot</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$extension</span></span><span>; </span><span><span class="hljs-comment">// Output: pdf</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
In this example, strrpos finds the position of the last dot in the filename, and substr extracts the substring after it to get the file extension.
Out-of-range $start and $length parameters: If $start exceeds the string length, or $length goes beyond the remaining characters, substr may return an empty string or the full string. Handle these situations carefully to avoid unintended errors.
Support for multibyte characters: PHP's substr may have limitations with multibyte characters such as Chinese. For multibyte strings, it is recommended to use mb_substr instead of substr.
substr is a very practical PHP tool that allows you to extract substrings from the left or even from the right. Proper use of this function can greatly simplify string handling, improve code readability, and increase efficiency. In actual development, substr can be flexibly used for various string extraction tasks, accommodating both fixed-length and dynamic conditions.