【发布时间】:2021-02-02 14:28:04
【问题描述】:
这是一个示例字符串:
{three / fifteen / one hundred} this is the first random number, and this is the second, {two / four}
在括号中,我需要返回一个随机值,例如:
One hundred is the first random number, and this is the second, two
我的代码:
function RandElement($str) {
preg_match_all('/{([^}]+)}/', $str, $matches);
return print_r($matches[0]);
}
$str = "{three/fifteen/one hundred} this is the first random number, and this is the second, {two/four}";
RandElement($str);
结果:
(
[0] => {three/fifteen/one hundred}
[1] => {two/four}
)
而且我不太明白下一步该做什么。从数组中取出第一个字符串并通过正则表达式将其传回?
【问题讨论】:
标签: php regex random preg-replace-callback