-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathreplace_with.php
32 lines (27 loc) · 1 KB
/
replace_with.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
declare(strict_types=1);
namespace Psl\Regex;
use Closure;
use function preg_replace_callback;
/**
* Perform a regular expression search and replace using a callback.
* Returns the `$haystack` string with all occurrences of `$pattern` replaced using
* `$callback`.
*
* @param non-empty-string $pattern The pattern to search for.
* @param (Closure(array<array-key, string>): string) $callback The replacement closure.
* @param null|positive-int $limit The maximum possible replacements for
* $pattern within $haystack.
*
* @throws Exception\InvalidPatternException If $pattern is invalid.
* @throws Exception\RuntimeException In case of an unexpected error.
*/
function replace_with(string $haystack, string $pattern, Closure $callback, null|int $limit = null): string
{
return (string) Internal\call_preg('preg_replace_callback', static fn(): string|null => preg_replace_callback(
$pattern,
$callback,
$haystack,
$limit ?? -1,
));
}