Add type-safe wrapper around preg_replace for replacing in strings
This commit is contained in:
parent
00fcc997ce
commit
523cdaa7f4
20
MD_STD.php
20
MD_STD.php
@ -115,6 +115,26 @@ class MD_STD {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user