String Functions

PHP strrpos() Function

Topic: PHP String ReferencePrev|Next

Description

The strrpos() function finds the position of the last occurrence of a string inside another string.

This function is case-sensitive. For case-insensitive searches, use the strripos() function.

The following table summarizes the technical details of this function.

Return Value: Returns the position of the last occurrence of a string within another string, or FALSE if the string was not found. Remember that string positions start at 0, not 1.
Version: PHP 4+

Syntax

The basic syntax of the strrpos() function is given with:

strrpos(string, search, start);

The following example shows the strrpos() function in action.

<?php
// Sample strings
$str = "The woodpeckers live in the woods.";
$substr = "The wood";

// Performing search
echo strrpos($str, $substr);
?>

Parameters

The strrpos() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to search in.
search Required. Specifies the string to search for.
start Optional. Specifies the position in the string to start searching from. If a negative value is specified, the search is performed right to left skipping the last start number of characters of the string and searching for the first occurrence of the search string.

More Examples

Here're some more examples showing how strrpos() function actually works:

The following example uses the start parameter to perform searching.

<?php
// Performing search
echo strrpos("The woodpeckers live in the woods.", "wood", 3); 
?>

In the following example the start parameter is set to a negative value.

<?php
// Performing search
echo strrpos("The woodpeckers live in the woods.", "wood", -10);
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties