text->getText(); preg_match_all($re, $newText, $found, PREG_SET_ORDER, 0); foreach ($found as $elements) { $data = explode(',', $elements[1]); $data = array_filter($data, function ($el) { return strlen(trim($el)); }); $data = array_values($data); $pos = strpos($newText, $elements[0]); $newText = substr_replace($newText, $data[mt_rand(0, count($data) - 1)], $pos, strlen($elements[0])); } return $newText; } }