witajcie
znalaazłem w sieci taki generator tekstów
liderman
autor twierdzi że wszystko powinno działać, a mi urywa teksty, nie losuje całego zdania, tylko bliżej nieokreślone fragmenty:
function getRandomPart($text) { $openLevel = 0; $lastPos = 0; $isIgnore = false; $textLen = mb_strlen($text, 'UTF-8'); for ($i = 0; $i < $textLen; $i++) { $currentChar = mb_substr($text, $i, 1, 'UTF-8'); if ($currentChar === '{') { $openLevel++; $isIgnore = true; continue; } if ($currentChar === '}') { $openLevel--; if ($openLevel === 0) { $isIgnore = false; } continue; } if ($isIgnore === true) { continue; } if ($currentChar === '|') { $parts[] = mb_substr($text, $lastPos, ($i - $lastPos), 'UTF-8'); $lastPos = $i + 1; } } $parts[] = mb_substr($text, $lastPos, null, 'UTF-8'); } function generate($text) { $startSafePos = 0; $startPos = 0; $endPos = 0; $openLevel = 0; $isFind = false; $result = ''; $textLen = mb_strlen($text, 'UTF-8'); for ($i = 0; $i < $textLen; $i++) { if (mb_substr($text, $i, 1, 'UTF-8') === '{') { if ($openLevel === 0) { $startPos = $i; $result .= mb_substr($text, $startSafePos, $startPos - $startSafePos, 'UTF-8'); } $openLevel++; continue; } if (mb_substr($text, $i, 1, 'UTF-8') === '}') { $openLevel--; if ($openLevel === 0) { $isFind = true; $endPos = $i; $startSafePos = $i + 1; $result .= generate( getRandomPart( mb_substr($text, $startPos + 1, $endPos - ($startPos + 1), 'UTF-8') ) ); continue; } } } if ($isFind === false) { return $text; } return $result . mb_substr($text, $endPos + 1, null, 'UTF-8'); } echo generate("{Good {morning|evening|day}|Goodnight|Hello}, {friend|brother}! {How are you|What's new with you}?");
wydaje mi się, że autor strywializował problem, ale może ktoś na forum wie jak poprawić te metody (wyłowiłem funkcje metod z klasy), albo podpowie mi dobry algorytm do tego, bo ja od kilku dni nad tym siedzę i nie mam pomysłu,
udało mi się tylko wymyślić walidację tego typu tekstów