$string = "Hello World Again". echo strrchr($string , ' '); // Gets ' Again'
Now I want to get "Hello World" from the $string
[The substring before the last occurrence of a space ' ' ]. How do I get it??
The strrpos() function finds the position of the last occurrence of a string inside another string. Note: The strrpos() function is case-sensitive. Related functions: strpos() - Finds the position of the first occurrence of a string inside another string (case-sensitive)
The strrpos() is an in-built function of PHP which is used to find the position of the last occurrence of a substring inside another string.
The strpos() function finds the position of the first occurrence of a string inside another string. Note: The strpos() function is case-sensitive.
strrchr() — Locate Last Occurrence of Character in String The strrchr() function finds the last occurrence of c (converted to a character) in string . The ending null character is considered part of the string . The strrchr() function returns a pointer to the last occurrence of c in string .
$string = "Hello World Again"; echo substr($string, 0, strrpos( $string, ' ') ); //Hello World
If the character isn't found, nothing is echoed
This is kind of a cheap way to do it, but you could split, pop, and then join to get it done:
$string = 'Hello World Again'; $string = explode(' ', $string); array_pop($string); $string = implode(' ', $string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With