Php get string before last occurrence of character
Now I want to get "Hello World" from the Show asked May 9, 2011 at 15:56
If the character isn't found, nothing is echoed answered May 9, 2011 at 16:03
meouwmeouw 41.2k10 gold badges51 silver badges69 bronze badges 3 This is kind of a cheap way to do it, but you could split, pop, and then join to get it done:
answered May 9, 2011 at 16:01
sdleihssirhcsdleihssirhc 41.3k5 gold badges52 silver badges67 bronze badges 1 One (nice and chilled out) way:
Other (more tricky) ways:
or
answered May 9, 2011 at 16:01
zafzaf 22.5k11 gold badges64 silver badges95 bronze badges 8 strripos — Find the position of the last occurrence of a case-insensitive substring in a string
answered Jan 19, 2015 at 7:23
R TR T 4,2093 gold badges35 silver badges47 bronze badges 1
answered May 9, 2011 at 16:03
TillTill 3,00416 silver badges18 bronze badges You can use a combination of strrpos, which gets the position of the last instance of a given string within a string, and substr to return the value. answered May 9, 2011 at 16:03
DaveKDaveK 4,4493 gold badges31 silver badges33 bronze badges The correct implementation should be:
Otherwise if the character is not found it will print nothing. See following case:
answered Oct 3, 2012 at 9:55
Arvind BhardwajArvind Bhardwaj 5,1935 gold badges35 silver badges48 bronze badges 0 You could just use:
This will work regardless of whether the character occurs in the string or not. It will also work if the last character is a space. answered Apr 7, 2013 at 23:30
1
answered Mar 5, 2014 at 14:50
answered Jun 4, 2021 at 10:33
How do you find the last occurrence of a character in a string in PHP?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)
How do I get everything before a character in PHP?PHP: Get Substring Before Space (Or Any Other Character). The PHP strtok() Function. The easiest way to get a substring before the first occurrence of a character such as a whitespace is to use the PHP strtok() function. ... . The PHP strstr() Function. ... . The PHP substr() Function. ... . PHP explode() + list() Functions.. What is Rtrim PHP?The rtrim() function removes whitespace or other predefined characters from the right side of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.
What is Strstr PHP?The strstr() function searches for the first occurrence of a string inside another string. Note: This function is binary-safe. Note: This function is case-sensitive. For a case-insensitive search, use stristr() function.
|