[php-src] Issue #22779: mb_strrpos() returns the wrong position for a negative offset in a non-UTF-8 encoding
[email protected] (eyupcanakman) Fri, 17 Jul 2026 01:22:37 +0000
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <[email protected]> |
Issue: https://github.com/php/php-src/issues/22779
Author: eyupcanakman
### Description
The following code:
```php
<?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