|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug3889; |
| 4 | + |
| 5 | +class HelloWorld |
| 6 | +{ |
| 7 | + public function sayHello(): void |
| 8 | + { |
| 9 | + $str = 'Hello'; |
| 10 | + $search = ['a', 'b', 'c']; |
| 11 | + $replace = ['x', 'y']; |
| 12 | + echo str_replace($search, $replace, $str); |
| 13 | + } |
| 14 | + |
| 15 | + public function replaceIsString(): void |
| 16 | + { |
| 17 | + $str = 'Hello'; |
| 18 | + $search = ['a', 'b', 'c']; |
| 19 | + echo str_replace($search, 'foo', $str); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class Variants |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @param list<string> $list |
| 27 | + * @param non-empty-list<string> $nonEmptyList |
| 28 | + * @param array{string, string} $two |
| 29 | + * @param array{string, string, string}|array{string} $oneOrThree |
| 30 | + */ |
| 31 | + public function doFoo(string $s, array $list, array $nonEmptyList, array $two, array $oneOrThree, bool $b): void |
| 32 | + { |
| 33 | + // see https://3v4l.org/Ct0Ts#v |
| 34 | + str_replace(['a', 'b'], ['x', 'y'], $s); |
| 35 | + str_replace(['a', 'b'], ['x', 'y', 'z'], $s); |
| 36 | + str_replace(['a', 'b'], [], $s); |
| 37 | + str_ireplace(['a', 'b', 'c'], ['x'], $s); |
| 38 | + str_replace('a', 'x', $s); |
| 39 | + str_replace($list, ['x'], $s); |
| 40 | + str_replace($nonEmptyList, ['x'], $s); |
| 41 | + str_replace($two, ['x'], $s); |
| 42 | + str_replace($two, $nonEmptyList, $s); |
| 43 | + str_replace($oneOrThree, ['x'], $s); |
| 44 | + str_replace($oneOrThree, [], $s); |
| 45 | + str_replace(['a', 'b', 'c'], $oneOrThree, $s); |
| 46 | + |
| 47 | + preg_replace(['/a/', '/b/'], ['x'], $s); |
| 48 | + preg_replace(['/a/', '/b/'], ['x', 'y'], $s); |
| 49 | + preg_replace(['/a/', '/b/'], 'x', $s); |
| 50 | + preg_filter(['/a/', '/b/'], ['x'], $s); |
| 51 | + |
| 52 | + $r = $b ? [] : ['x']; |
| 53 | + str_replace(['a', 'b', 'c'], $r, $s); |
| 54 | + |
| 55 | + $r = $b ? ['x'] : ['x', 'y', 'z']; |
| 56 | + str_replace(['a', 'b', 'c'], $r, $s); |
| 57 | + |
| 58 | + str_replace(['a', 'b', 'c'], ['x', 'y', 'z'], $s); |
| 59 | + } |
| 60 | +} |
0 commit comments