Backend Development 6 min read

Understanding PHP strrpos() Function: Syntax, Parameters, Return Values, and Practical Examples

This article explains the PHP strrpos() function, detailing its syntax, parameters, return values, and providing multiple code examples that demonstrate how to locate the last occurrence of characters or substrings within a string, including handling of offsets and missing matches.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding PHP strrpos() Function: Syntax, Parameters, Return Values, and Practical Examples

The strrpos() function in PHP is used to find the position of the last occurrence of a specified character or substring within a string, offering flexible options for efficient string handling.

Basic Syntax of strrpos()

<code>int strrpos ( string $haystack , mixed $needle [, int $offset = 0 ] )</code>

$haystack : The string to be searched.

$needle : The character or substring to look for.

$offset : Optional; the position in the string to start searching from.

The function returns an integer indicating the position of the last occurrence; if the character or substring is not found, it returns false .

Usage of strrpos()

Example 1: Find the last occurrence of a character

<code>$str = "Hello, World!";<br/>$position = strrpos($str, "o");<br/>echo "The last occurrence of 'o' is at: " . $position;</code>

Output:

<code>The last occurrence of 'o' is at: 8</code>

In this example, the character o appears last at position 8 in the string "Hello, World!" .

Example 2: Find the last occurrence of a substring

<code>$str = "Hello, World!";<br/>$position = strrpos($str, "or");<br/>echo "The last occurrence of 'or' is at: " . $position;</code>

Output:

<code>The last occurrence of 'or' is at: 7</code>

Here, the substring or last appears at position 7.

Example 3: Search starting from a specific offset

<code>$str = "Hello, World!";<br/>$position = strrpos($str, "o", 5);<br/>echo "Starting from offset 5, the last 'o' is at: " . $position;</code>

Output:

<code>Starting from offset 5, the last 'o' is at: 4</code>

When the search begins at offset 5, the character o is found last at position 4.

Example 4: Handling a character that is not found

<code>$str = "Hello, World!";<br/>$position = strrpos($str, "z");<br/>if ($position === false) {<br/>    echo "Character 'z' not found";<br/>} else {<br/>    echo "The last occurrence of 'z' is at: " . $position;<br/>}</code>

Output:

<code>Character 'z' not found</code>

This demonstrates that when the target character or substring does not exist in the string, strrpos() returns false , allowing developers to handle the case gracefully.

Conclusion

The strrpos() function is a simple yet powerful tool in PHP for locating the final position of characters or substrings within strings. By providing the string to search, the target needle, and an optional offset, developers can efficiently perform reverse searches and handle cases where the needle is absent.

backend developmentPHPTutorialstring-functionsstrrpos
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.