Looking for a class function to iterate through all possible variants of a line

0 like 0 dislike
5 views
Good afternoon everyone.

Looking for a class (function) in PHP to iterate through all possible variants of a line formed from such structures {||}.

Absolutely great would be if Robin will go from the most unique line to the most unique.
by | 5 views

2 Answers

0 like 0 dislike
Recently on Habre slipped a crop top. The link is not kept, only the code:
\r
<?\r\r$str = "{Пожалуйста|Просто} сделайте так, чтобы это {удивительное|крутое|простое} тестовое предложение {изменялось {быстро|мгновенно} случайным образом|менялось каждый раз}";\r$finishStrs = array();\r\rgetVariants($str);\r\rfunction getVariants($str) {\rglobal $finishStrs;\r$strs = array();\r\r\rif(preg_match("~\\{([^{}]+)\\}~siU", $str, $m)) {\r\r$foundExpression = $m[1];\r\r$currentVariants = explode("|", $foundExpression);\r\rforeach($currentVariants as $var) {\r$strs[] = str_replace('{'.$foundExpression.'}', $var, $str);\r}\r\rforeach($strs as $currentStr) {\rif(preg_match("~\\{([^{}]+)\\}~siU", $currentStr, $m)) {\rgetVariants($currentStr);\r} else {\r$finishStrs[] = $currentStr;\r}\r}\r\r} else {\r$finishStrs = $str;\r}\r\r\r}\r\rprint_r($finishStrs);\r</code>
by
0 like 0 dislike
those who suddenly came here by googling my problem in the job — this algorithm does not take into account the positional feature of entry and is not valid in the General case (not for this specific line).
by

Related questions

0 like 0 dislike
3 answers
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
3 answers
110,608 questions
257,186 answers
0 comments
28,667 users