/** * Increments username - increments trailing number or adds it if not present. * Varifies that the new username does not exist yet * @param string $username * @return incremented username which does not exist yet */ function increment_username($username, $mnethostid) { global $DB; if (!preg_match_all('/(.*?)([0-9]+)$/', $username, $matches)) { $username = $username . '2'; } else { $username = $matches[1][0] . ($matches[2][0] + 1); } if ($DB->record_exists('user', array('username' => $username, 'mnethostid' => $mnethostid))) { return increment_username($username, $mnethostid); } else { return $username; } }
/** * Increments username - increments trailing number or adds it if not present. * Varifies that the new username does not exist yet * @param string $username * @return incremented username which does not exist yet */ function increment_username($username, $mnethostid) { if (!preg_match_all('/(.*?)([0-9]+)$/', $username, $matches)) { $username = $username . '2'; } else { $username = $matches[1][0] . ($matches[2][0] + 1); } if (record_exists('user', 'username', addslashes($username), 'mnethostid', addslashes($mnethostid))) { return increment_username($username, $mnethostid); } else { return $username; } }