Php preg_match()替换某些文本中的内容

Php preg_match()替换某些文本中的内容,php,preg-replace,preg-match,Php,Preg Replace,Preg Match,有人能帮我吗 正文: 这是一个带有数字[RANDOM:100200]的示例文本,该数字是由函数生成的 结果: 这是由函数生成的数字为153的示例文本 我想搜索并将此模式从[RANDOM:100200]替换为100-200的数字 谢谢大家 $str = "this is my script [random:15;30] and I want to see if I found that."; preg_match("/random:(.*)/", $str, $res); print_r($res

有人能帮我吗

正文: 这是一个带有数字[RANDOM:100200]的示例文本,该数字是由函数生成的

结果: 这是由函数生成的数字为153的示例文本

我想搜索并将此模式从[RANDOM:100200]替换为100-200的数字

谢谢大家

$str = "this is my script [random:15;30] and I want to see if I found that.";
preg_match("/random:(.*)/", $str, $res);
print_r($res);

也许是这样的

$str = "this is my script [random:15;30] and I want to see if I found that.";
preg_match("/\[random:(\d+);(\d+)\]/", $str, $res);

$random = rand($res[1], $res[2]);
$str = str_replace($res[0], $random, $str);

echo $str;

到目前为止,您试图如何实现这一目标?为其分配一个变量,然后对其应用搜索模式。我已经添加了我尝试过的内容。我的问题是如何在文本中找到[RANDOM:100200]。。我找不到合适的模式。。