I am using php 5 to parse a string. My input string looks like the following:
{Billion is|Millions are|Trillion is} {an extremely |a| a generously | a very} { tiny|little |smallish |short |small} stage in a vast {galactic| |large|huge|tense|big |cosmic} {universe|Colosseum|planet|arena}.
Find below my minimum viable example:
<?php
function process($text)
{
return preg_replace_callback('/\[(((?>[^\[\]]+)|(?R))*)\]/x', array(
$this,
'replace'
), $text);
}
function replace($text)
{
$text = $this->process($text[1]);
$parts = explode('|', $text);
return $parts[array_rand($parts)];
}
$text = "{Billion is|Millions are|Trillion is} {an extremely |a| a generously | a very} { tiny|little |smallish |short |small} stage in a vast {galactic| |large|huge|tense|big |cosmic} {universe|Colosseum|planet|arena}.";
$res = process($text);
echo $res;
As you can see I am trying to parse the following pattern f.ex.: {Billion is|Millions are|Trillion is}
using the above regex, /\[(((?>[^\[\]]+)|(?R))*)\]/x
.
As a result I am getting the same string as inputted. I would like to get as an output for example:
Billion is a very little stage in a vast huge arena.
Any suggestions what I am doing wrong?