PHP strrpos()

strrpos() function compare two strings, return the last occurrence position of the 2nd string inside the 1st string. If the 1st string do not contain the 2nd string, return FALSE.

<?PHP
	echo strrpos("The best time is no time at that time","time");  //33
	if (strrpos("The best time is no time at that time","PHP"))
	{
	   echo "contain PHP";
	}
	else echo "do not contain PHP";
	//do not contain PHP
	echo strrpos("The best time is no time at that time","The");  //0
?>

strrpos() function is case sensitive.

<?PHP
	if (strrpos("The best time is no time at that time","TIME"))
	{
	   echo "contain TIME";
	}
	else echo "do not contain TIME";
	//do not contain TIME
	if (strrpos("The best time is no time at that time","time"))
	{
	   echo "contain time";
	}
	else echo "do not contain time";
	//contain time
?>

If the occurrence position is the 1st character position, will return 0. That may cause wrong results when used in a condition statement.

<?PHP
	if (strrpos("The best time is no time at that time","The"))
	{
	   echo "contain The";
	}
	else echo "do not contain The";
	//do not contain The
?>

The correct statement is:

<?PHP
	if (strrpos("The best time is no time at that time","The") === FALSE)
	{
	   echo "do not contain The";
	}
	else echo "contain The";
	//contain The
?>



:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • regular expr. • str_replace
 • strcmp • strpos
 • strrev • strrpos
 • strtr • substr
 • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expr. • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2024  | Terms of Use | Privacy | Home