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