3v4l.org

run code in 200+ PHP versions simultaneously
<?php function randomString(int $stringLength, array $parts, string $delimeter = ' ') : string { $keys = array_keys($parts); // Получаем массив ключей массива. Они упорядочены и идут с нулевого индекса. $count = count($keys); if (0 === $count) { return ''; } $text = []; for($i = 1; $i <= $stringLength; $i++) { // Больше нравится когда написано конкретно "с первого до последнего" $text[] = $parts[$keys[random_int(0, $count - 1)]]; // Получаем значение по случайному ключу из массива ключей } return implode($delimeter, $text); } echo randomString( 10, [ 'a' => 'lorem', 14 => 'ipsum', 'dolor' => 'dolor', 5 => 'sit', 'amet' => 'amet' ] );
based on eP0jX
Output for 7.4.1
ipsum ipsum amet ipsum lorem sit ipsum dolor lorem dolor
Output for 7.4.0
dolor dolor dolor ipsum ipsum ipsum sit amet sit amet
Output for 7.3.13
dolor dolor lorem amet sit ipsum dolor amet sit sit
Output for 7.3.12
ipsum dolor dolor lorem amet sit lorem sit sit sit
Output for 7.3.11
sit sit lorem ipsum lorem lorem sit dolor amet dolor
Output for 7.3.10
amet ipsum dolor ipsum lorem lorem dolor sit lorem amet
Output for 7.3.9
sit dolor sit dolor lorem amet dolor amet lorem dolor
Output for 7.3.8
dolor sit dolor lorem ipsum sit dolor ipsum lorem sit
Output for 7.3.7
dolor ipsum dolor ipsum amet dolor dolor ipsum sit dolor
Output for 7.3.6
amet amet amet ipsum amet amet sit sit ipsum amet
Output for 7.3.5
dolor lorem dolor lorem sit sit ipsum dolor ipsum ipsum
Output for 7.3.4
amet sit dolor amet dolor dolor sit dolor dolor amet
Output for 7.3.3
lorem amet ipsum amet sit ipsum lorem sit ipsum lorem
Output for 7.3.2
lorem sit amet lorem dolor dolor dolor ipsum sit dolor
Output for 7.3.1
ipsum dolor sit dolor amet amet dolor amet dolor amet
Output for 7.3.0
amet sit lorem amet amet amet ipsum ipsum lorem ipsum
Output for 7.2.26
ipsum dolor lorem sit lorem ipsum lorem ipsum amet sit
Output for 7.2.25
dolor amet sit amet lorem dolor dolor sit lorem dolor
Output for 7.2.24
lorem lorem sit sit sit lorem amet lorem amet sit
Output for 7.2.23
ipsum sit lorem ipsum dolor sit dolor ipsum dolor amet
Output for 7.2.22
dolor dolor amet lorem ipsum sit sit sit sit amet
Output for 7.2.21
lorem ipsum amet lorem ipsum ipsum lorem ipsum dolor ipsum
Output for 7.2.20
ipsum amet dolor ipsum lorem dolor ipsum amet sit sit
Output for 7.2.19
ipsum lorem amet dolor amet ipsum ipsum ipsum ipsum ipsum
Output for 7.2.18
amet dolor amet dolor dolor ipsum lorem sit sit dolor
Output for 7.2.17
amet dolor lorem ipsum amet ipsum dolor amet lorem ipsum
Output for 7.2.16
sit amet sit sit ipsum dolor lorem dolor ipsum sit
Output for 7.2.15
amet lorem lorem sit ipsum lorem sit ipsum dolor dolor
Output for 7.2.14
amet ipsum ipsum dolor amet sit dolor amet lorem ipsum
Output for 7.2.13
amet lorem dolor lorem lorem sit dolor dolor lorem lorem
Output for 7.2.12
amet dolor lorem dolor sit sit amet dolor lorem lorem
Output for 7.2.11
lorem ipsum sit amet dolor dolor sit sit dolor ipsum
Output for 7.2.10
sit lorem dolor amet amet lorem dolor ipsum amet dolor
Output for 7.2.9
amet dolor dolor sit ipsum dolor dolor dolor lorem dolor
Output for 7.2.8
ipsum ipsum dolor lorem lorem ipsum lorem lorem amet lorem
Output for 7.2.7
amet dolor amet dolor dolor amet sit amet lorem dolor
Output for 7.2.6
dolor amet amet ipsum amet dolor ipsum amet dolor amet
Output for 7.2.5
ipsum amet sit amet amet dolor amet sit ipsum dolor
Output for 7.2.4
amet amet dolor dolor dolor amet amet amet ipsum sit
Output for 7.2.3
sit ipsum sit lorem lorem amet sit sit amet amet
Output for 7.2.2
lorem dolor sit ipsum sit lorem lorem sit lorem sit
Output for 7.2.1
lorem ipsum sit sit ipsum dolor sit dolor amet amet
Output for 7.2.0
sit sit ipsum dolor sit amet ipsum ipsum amet ipsum