In PHP, string manipulation is one of the most common and important tasks. Many times, we need to extract a specific part of a string. The strrpos and substr functions are very practical PHP functions that can be combined to extract content starting from the last character position to the end of the string.
The strrpos function is used to find the position of the last occurrence of a character or substring within a string. It returns the offset (the distance from the beginning of the string to the found character). If the character is not found, it returns false.
<span><span><span class="hljs-title function_ invoke__">strrpos</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$haystack</span></span>, </span><span><span class="hljs-keyword">mixed</span></span> </span><span><span class="hljs-variable">$needle</span></span>, </span><span><span class="hljs-keyword">int</span></span> </span><span><span class="hljs-variable">$offset</span></span> = </span><span><span class="hljs-number">0</span></span>): </span><span><span class="hljs-keyword">int</span></span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
$haystack: The string to search in.
$needle: The character or substring to find.
$offset: Optional. The position to start searching from. Default is 0.
The substr function is used to extract a substring from a string. It works by specifying the start position and length.
<span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-keyword">string</span></span> </span><span><span class="hljs-variable">$string</span></span>, </span><span><span class="hljs-keyword">int</span></span> </span><span><span class="hljs-variable">$start</span></span>, </span><span><span class="hljs-keyword">int</span></span> </span><span><span class="hljs-variable">$length</span></span> = ?): </span><span><span class="hljs-keyword">string</span></span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
$string: The original input string.
$start: The starting position of extraction. Can be positive or negative. A negative value counts from the end of the string.
$length: Optional. The number of characters to extract. If omitted, it extracts to the end of the string.
Now we will combine strrpos and substr to extract content from the string. The goal is: start from the last occurrence of a specific character in the string and extract until the end.
Suppose we have a string $str = "apple, banana, cherry, apple, orange". We want to extract from the last occurrence of "apple" to the end. First, we use strrpos to find the position of the last "apple".
<span><span><span class="hljs-variable">$str</span></span> = </span><span><span class="hljs-string">"apple, banana, cherry, apple, orange"</span></span>;
</span><span><span class="hljs-variable">$last_pos</span></span> = </span><span><span class="hljs-title function_ invoke__">strrpos</span></span>(</span><span><span class="hljs-variable">$str</span></span>, </span><span><span class="hljs-string">"apple"</span></span>); </span><span><span class="hljs-comment">// Find the last occurrence of "apple"</span></span>
</span></span>
Now $last_pos holds the position of the last occurrence of "apple" in the string, which is 24.
Next, we use the substr function starting from $last_pos to the end of the string.
<span><span><span class="hljs-variable">$result</span></span> = </span><span><span class="hljs-title function_ invoke__">substr</span></span>(</span><span><span class="hljs-variable">$str</span></span>, </span><span><span class="hljs-variable">$last_pos</span></span>); </span><span><span class="hljs-comment">// Extract starting from the last "apple"</span></span>
</span></span>
At this point, $result equals "apple, orange", which is the content from the last "apple" to the end of the string.
<span><span><span class="hljs-meta"><?php</span></span>
</span><span><span class="hljs-comment">// Original string</span></span>
</span><span><span class="hljs-variable">$str</span></span> = </span><span><span class="hljs-string">"apple, banana, cherry, apple, orange"</span></span>;
<p></span>// Find the last occurrence of "apple"<br>
</span>$last_pos = </span>strrpos(</span>$str, </span>"apple");</p>
<p></span>// Check if the character was found<br>
</span>if (</span>$last_pos !== </span>false) {<br>
</span>// Extract from the last "apple" to the end of the string<br>
</span>$result = </span>substr(</span>$str, </span>$last_pos);<br>
</span>echo </span>"Extracted content: " . </span>$result;<br>
} </span>else {<br>
</span>echo </span>"Specified character not found";<br>
}<br>
</span>?><br>
</span></span>
<span><span><span class="hljs-section">Extracted content: apple, orange</span></span>
</span></span>
By combining the strrpos and substr functions, we can easily extract content from a string starting at the last occurrence of a character or substring all the way to the end. strrpos helps us locate the last character position, while substr retrieves the remaining content from that point onward.