Add type-safe wrapper around preg_replace for replacing in strings

This commit is contained in:
Joshua Ramon Enslin 2020-08-09 00:59:41 +02:00 committed by Stefan Rohde-Enslin
parent 00fcc997ce
commit 523cdaa7f4

View File

@ -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.
*