lundi 19 octobre 2020

Replace curly braced expression with one item from the expression

Here is a sample string:

{three / fifteen / one hundred} this is the first random number, and this is the second, {two / four}

From the brackets, I need to return a random value for example:

One hundred is the first random number, and this is the second, two

My code:

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);

Result:

(
    [0] => {three/fifteen/one hundred}
    [1] => {two/four}
)

And I don't quite understand what to do next. Take the first string from an array and pass it back through a regex?




Aucun commentaire:

Enregistrer un commentaire