I have a code like this to find a pattern for example ##random_number_10## I need to take the number 10 to be used as a parameter in the function and that function will go to str_replace to replace the pattern
string :
ini subjek lur ##random_mix_5## ##random_string_up_10## ##random_number_15##
output :
ini subjek lur o1ovp HDBVPVJTYY 741007211142434
.
function replace ($sumber) {
preg_match_all("/##random_number_(\d+)##/", $sumber, $match_number);
preg_match_all("/##random_string_up_(\d+)##/", $sumber, $match_string_up);
preg_match_all("/##random_string_low_(\d+)##/", $sumber, $match_string_low);
preg_match_all("/##random_string_uplow_(\d+)##/", $sumber, $match_string_uplow);
preg_match_all("/##random_mix_(\d+)##/", $sumber, $match_mix);
$dari = [
$match_number[0][0],
$match_string_up[0][0],
$match_string_low[0][0],
$match_string_uplow[0][0],
$match_mix[0][0]
];
$ke = [
random_number($match_number[1][0]),
random_string_up($match_string_up[1][0]),
random_string_low($match_string_low[1][0]),
random_string_uplow($match_string_uplow[1][0]),
random_mix($match_mix[1][0])
];
return str_replace($dari, $ke, $sumber);
};
the problem is when I try to replace all of them an error will appear if the string does not contain one of the searches. is there another way to replace the pattern on the string?