Skip to content

mb_strrpos() returns the wrong position for a negative offset in a non-UTF-8 encoding #22779

Closed

Description

@eyupcanakman

Description

The following code:

<?php
var_dump(mb_strrpos("\xA9\xA9X", "\xA9", -2, "ISO-8859-1"));
var_dump(strrpos("\xA9\xA9X", "\xA9", -2));

Resulted in this output:

int(0)
int(1)

But I expected this output instead:

int(1)
int(1)

For a single-byte encoding one byte is one character, so mb_strrpos() should agree with strrpos() here. The result is wrong for a negative offset in any non-UTF-8 encoding. An ISO-8859-1 needle byte in 0x80-0xBF gives a position that is too early, and an ASCII needle in UTF-16 also returns the wrong position.

PHP Version

PHP 8.4.25-dev (the same code is in 8.3, 8.5 and master)

Operating System

macOS 27.0

Activity

  1. youkidearitai commented on Jul 17, 2026

    @youkidearitai
    Contributor

    Thanks for report. I confirmed behavior changes from PHP 8.3
    https://3v4l.org/MpZOD#veol

    @alexdowad What do you think?

  2. alexdowad commented on Jul 17, 2026

    @alexdowad
    Contributor

    @youkidearitai I commented in @eyupcanakman's PR.

  3. added a commit that references this issue on Jul 23, 2026
    0d6ab53
  4. added a commit that references this issue on Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions