Skip to content

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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions