<?php
namespace Example;
function substr_replace(
array|string $string,
array|string $replace,
array|int|null $offset,
array|int|null $length = null
): string|array {
if ( $offset === null ) {
$offset = is_string($string)
? strlen($string)
: array_map(strlen(...), $string);
}
return \substr_replace($string, $replace, $offset, $length);
}
$foo = substr_replace('abc', 'xyz', null);
var_dump($foo);
$foo = substr_replace(['hello', 'goodbye'], '!', null);
var_dump($foo);
$foo = substr_replace(['one', 'two'], [' - uno', ' - dos'], null);
var_dump($foo);
- Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- string(6) "abcxyz"
array(2) {
[0]=>
string(6) "hello!"
[1]=>
string(8) "goodbye!"
}
array(2) {
[0]=>
string(9) "one - uno"
[1]=>
string(9) "two - dos"
}
preferences:
63.93 ms | 406 KiB | 5 Q