<?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.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
- 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:
177.28 ms | 1400 KiB | 58 Q