xref: /PHP-8.1/ext/mbstring/tests/bug43841.phpt (revision a06d015e)
1--TEST--
2Test mb_strrpos() function : mb_strrpos offset is byte count for negative values
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7/*
8 * Test that mb_strrpos offset is byte count for negative values (should be character count)
9 */
10
11$offsets = array(-25, -24, -13, -12);
12// Japanese string in UTF-8
13$string_mb = "日本語テキストです。0123456789。";
14$needle = "。";
15
16foreach ($offsets as $i) {
17    echo "\n-- Offset is $i --\n";
18    echo "Multibyte String:\n";
19    try {
20        var_dump( mb_strrpos($string_mb, $needle, $i, 'UTF-8') );
21    } catch (\ValueError $e) {
22        echo $e->getMessage() . \PHP_EOL;
23    }
24    echo "ASCII String:\n";
25    echo "mb_strrpos:\n";
26    try {
27        var_dump(mb_strrpos('This is na English ta', 'a', $i));
28    } catch (\ValueError $e) {
29        echo $e->getMessage() . \PHP_EOL;
30    }
31    echo "strrpos:\n";
32    try {
33        var_dump(strrpos('This is na English ta', 'a', $i));
34    } catch (\ValueError $e) {
35        echo $e->getMessage() . \PHP_EOL;
36    }
37}
38?>
39--EXPECT--
40-- Offset is -25 --
41Multibyte String:
42mb_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
43ASCII String:
44mb_strrpos:
45mb_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
46strrpos:
47strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
48
49-- Offset is -24 --
50Multibyte String:
51mb_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
52ASCII String:
53mb_strrpos:
54mb_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
55strrpos:
56strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
57
58-- Offset is -13 --
59Multibyte String:
60bool(false)
61ASCII String:
62mb_strrpos:
63bool(false)
64strrpos:
65bool(false)
66
67-- Offset is -12 --
68Multibyte String:
69int(9)
70ASCII String:
71mb_strrpos:
72int(9)
73strrpos:
74int(9)
75