Content · Programming · PHP

PHP mb_strrpos() Function: Usage and Examples

Learn how to find the last case-sensitive character position in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to find the last case-sensitive character position. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

mb_strrpos: This finds the final occurrence of a substring using character offsets.

What Is mb_strrpos in PHP?

Find the last case-sensitive character position. This finds the final occurrence of a substring using character offsets.

Syntax:

mb_strrpos(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): int|false

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: The last occurrence may be at position zero; do not use a loose truthiness check.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

var_dump(mb_strrpos("one two one", "one", 0, "UTF-8"));

The code focuses on the main operation. This finds the final occurrence of a substring using character offsets.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

var_dump(mb_strrpos("file.txt", ".", 0, "UTF-8"));

What changes? The last occurrence may be at position zero; do not use a loose truthiness check.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

$filenames = ["report.final.pdf", "archive.tar.gz"];
foreach ($filenames as $filename) {
    $lastDot = mb_strrpos($filename, ".", 0, "UTF-8");
    if ($lastDot !== false) {
        echo mb_substr($filename, $lastDot + 1, null, "UTF-8") . PHP_EOL;
    }
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. This finds the final occurrence of a substring using character offsets.

Important Behavior and Edge Cases

  • Core behavior: This finds the final occurrence of a substring using character offsets.
  • Inputs and results: The last occurrence may be at position zero; do not use a loose truthiness check.
  • Alternatives: mb_strpos() finds the first matching occurrence.
  • Compatibility: mbstring is required for mb_* functions.

Comparison With Related Functions

Function or ApproachWhen to Use It
mb_strrposFind the last case-sensitive character position.
Related PHP approachmb_strpos() finds the first matching occurrence.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionThe last occurrence may be at position zero; do not use a loose truthiness check.
Confusing this API with a related operationmb_strpos() finds the first matching occurrence.
Assuming output is identical across environmentsCheck return values and required extensions or versions. mbstring is required for mb_* functions.

Key Takeaways

  • mb_strrpos helps you find the last case-sensitive character position.
  • This finds the final occurrence of a substring using character offsets.
  • The last occurrence may be at position zero; do not use a loose truthiness check.

Use mb_strrpos when you need to find the last case-sensitive character position. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

Share this resourceSend this page to someone who may find it useful.