MyBB Community Forums

Full Version: "format_name()" function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, please could you tell me how the format_name function works, and how the variables inside are processed.

If you don't know exactly what I'm trying to say, here's an example.

$formattedname = format_name($row['username'], $row['usergroup'], $row['displaygroup']);

I understand that the above string will grab the user's user group and display group and then format the name accordingly.

Is there anyway to add other variables to that, say for example, I wanted to format a username based on a specific color?

Many thanks.
This is the function, play with it:

/**
 * Formats a username based on their display group
 *
 * @param string The username
 * @param int The usergroup for the user (if not specified, will be fetched)
 * @param int The display group for the user (if not specified, will be fetched)
 * @return string The formatted username
 */
function format_name($username, $usergroup, $displaygroup="")
{
	global $groupscache, $cache;

	if(!is_array($groupscache))
	{
		$groupscache = $cache->read("usergroups");
	}

	if($displaygroup != 0)
	{
		$usergroup = $displaygroup;
	}

	$ugroup = $groupscache[$usergroup];
	$format = $ugroup['namestyle'];
	$userin = substr_count($format, "{username}");

	if($userin == 0)
	{
		$format = "{username}";
	}

	$format = stripslashes($format);

	return str_replace("{username}", $username, $format);
}
Cheers for the reply. All sorted now, many thanks.
You may want to change the way you format user names based on group (ACP => Users and Groups => Groups => Edit Group => Username Style). I would surround {username} with a span with a CSS class which you can edit for each theme. As an example, my Administrators group is defined as <span class="users_admin">{username}</span> and my theme defines a font color, size, and weight.