What are the benefits of using substr() and strrpos() functions in PHP for string manipulation?
When working with strings in PHP, sometimes we need to extract a portion of a string or find the position of the last occurrence of a substring within a string. The substr() function allows us to extract a portion of a string based on a specified start and length, while the strrpos() function helps us find the position of the last occurrence of a substring within a string. These functions are useful for manipulating strings and extracting specific information from them.
// Using substr() and strrpos() functions for string manipulation
$string = "Hello, World!";
$substring = substr($string, 0, 5); // Extracts "Hello"
$lastOccurrence = strrpos($string, "o"); // Finds the position of the last occurrence of "o" in the string
echo $substring . "\n"; // Output: Hello
echo $lastOccurrence; // Output: 8