PHP iconv_strrpos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iconv

Definition

In PHP, iconv_strrpos() finds the last place where a piece of text appears in a string and returns its character position.

It counts characters, not bytes, and it searches from the end of the string.

Syntax

Call iconv_strrpos() with the values you need. Start with this line.

PHP
$pos = iconv_strrpos($haystack, $needle, "UTF-8");
CallWhat it does
iconv_strrpos("a.b.c", ".", "UTF-8")Returns 3.
iconv_strrpos("abc", "z", "UTF-8")Returns false.

This function has no offset argument. The encoding is the third argument.

Library declarations
PHP
iconv_strrpos(string $haystack, string $needle, ?string $encoding = null): int|false

Parameters

ParameterDescription
haystackRequired. The text to search in.
needleRequired. The text to look for.
encodingOptional. The character set. Default is the iconv internal encoding.

Return Value

Return ValueDescription
int|falseThe character position of the last match, or false if not found.

PHP Examples

Example 1 — Find the Last Letter

Find the last l in a text with accents.

PHP
<?php
$text = "Héllo Wörld, hello";

echo iconv_strrpos($text, "l", "UTF-8"), PHP_EOL;

How It Works

  1. The text has several l letters.
  2. The search starts at the end.
  3. The last l is the 17th character, so its position is 16.
  4. Positions start at 0 and count letters, not bytes.

Example 2 — Handle No Match

A missing needle gives false.

PHP
<?php
var_dump(iconv_strrpos("Héllo", "z", "UTF-8"));

How It Works

  1. Search for a letter that is not there.
  2. The function returns false.
  3. Always test with === false.
  4. Position 0 is a valid result.

Example 3 — Get a File Extension

Find the last dot and read the text after it.

PHP
<?php
$file = "résumé.final.pdf";

$dot = iconv_strrpos($file, ".", "UTF-8");

echo $dot, PHP_EOL;
echo iconv_substr($file, $dot + 1, null, "UTF-8"), PHP_EOL;

How It Works

  1. The file name has two dots. The last one starts the extension.
  2. Find it with iconv_strrpos(). The position is 12.
  3. Cut the text after the dot with iconv_substr().
  4. You get pdf. Accents in the name do not matter.

Frequently Asked Questions

iconv_strrpos() returns the character position of the last match of a piece of text in a string.
iconv_strrpos(string $haystack, string $needle, ?string $encoding = null): int|false.
No. Only iconv_strpos() accepts an offset.
It returns false.
Yes. mb_strrpos() does the same job and also accepts an offset.

Next: iconv_substr()

iconv_substr() returns part of a string by character position.

iconv_substr() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful