Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strrchr issue using capitalised characters 1

Status
Not open for further replies.

monkey64

Technical User
Apr 27, 2008
69
GB
The strrchr() function finds the position of the last occurrence of a string within another string, and returns all characters from this position to the end of the string.

My code:
Code:
echo strrchr("I am searching for OBSVALUE on my text", "OBSVALUE") . "<br>";
echo strrchr("I am searching for OBSVALUE ON my text", "OBSVALUE");

In the first example, I get "OBSVALUE on my text" and that's what I would expect.
In the second, I get "ON my text". I was expecting the same.

Any ideas why there is a variation?

 
the function does not behave as you would expect. note the sentence in the manual

php manual said:
If needle contains more than one character, only the first is used. This behavior is different from that of strstr().

if you want to get the remains of a string try this instead
Code:
<?php
function getEndBit($haystack, $needle, $caseInsensitive=false){
	if ($caseInsensitive){
		$haystack = strtolower($haystack);
		$needle = strtolower($needle);
	}
	$bits = explode($needle, $haystack);
	if (count ($bits) == 0) return false;
	return $needle . end($bits);
}

echo getEndBit("I am searching for OBSVALUE on my text", "OBSVALUE") . "<br>";
echo getEndBit("I am searching for OBSVALUE ON my text", "OBSVALUE");
?>

Code:
OBSVALUE on my text
OBSVALUE ON my text
 
Fantastic jpadie!
Well I never knew that! My book doesn't really go into details. Your code is good.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top