Add type-safe wrapper around preg_replace for replacing in strings
This commit is contained in:
1 parent
00fcc997ce
commit
523cdaa7f4
1 file changed
+20
+20
@@ -115,6 +115,26 @@ public static function startsWithAny(string $haystack, array $needles):bool {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-safe(r) wrapper around preg_replace.
|
||||||
|
*
|
||||||
|
* @param string $pattern The pattern to search for. It can be either a string or an array with strings.
|
||||||
|
* @param string $replacement To replace with.
|
||||||
|
* @param string $subject The string or an array with strings to search and replace.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function preg_replace_str(string $pattern, string $replacement, string $subject):string {
|
||||||
|
|
||||||
|
$output = preg_replace($pattern, $replacement, $subject);
|
||||||
|
if ($output === null) {
|
||||||
|
throw new Exception("Error replacing in $subject: Replacing $pattern with $replacement");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Type-safe wrapper around json_encode.
|
* Type-safe wrapper around json_encode.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in new issue
Block a user