MyBB Community Forums

Full Version: What does this syntax mean?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Well I browsed through mybb's lang files and found something I dont quite understand:

$l['local_time_format'] = "{1} at {2}";
$l['users_forum_info'] = "{1}'s Forum Info";

What do those {1} and {2} mean? And how are they being used in actual scripts? I am a bit confused, can anyone please explain this syntax for me? o_o
They're placeholders. {1} will be replaced with whatever the second parameter is, and {2} will be replaced with whatever the third parameter is, respectively and so on.

Example:

$lang->users_forum_info = $lang->sprintf($lang->users_forum_info, $memprofile['username']);

If the user's username is 'admin' is will output admin's Forum Info.
I see, but I thought only specific PHP extensions can use placeholders, such as PDO. How does MyBB get it to work? Did you use an extension?
We use this simple function:

	function sprintf($string)
	{
		$arg_list = func_get_args();
		$num_args = count($arg_list);
		
		for($i = 1; $i < $num_args; $i++)
		{
			$string = str_replace('{'.$i.'}', $arg_list[$i], $string);
		}
		
		return $string;
	}
Yeah it's literally a case of the {1} being replaced with the first replacement you provide, {2} with the second...

$replacement_1 = 'Matt';
$replacement_2 = '20';
$l['my_language_string'] = 'Hello, my name is {1} and I am {2}';
echo $lang->sprintf($lang->my_language_string, $replacement_1, $replacement_2);
// Hello, my name is Matt and I am 20
Thanks a lot for your responses, much appreciated. I still dont quite understand how the method lang::sprintf() accepts one argument called $string while in your example there was three arguments $lang->my_language_string, $replacement_1 and $replacement_2. Would you mind explaining a bit about this? Thx.
(2012-06-03, 08:26 AM)Hall of Famer Wrote: [ -> ]Thanks a lot for your responses, much appreciated. I still dont quite understand how the method lang::sprintf() accepts one argument called $string while in your example there was three arguments $lang->my_language_string, $replacement_1 and $replacement_2. Would you mind explaining a bit about this? Thx.

The first was the language string, or basically the array key... Sorta... The second and third were the replacements.
Because our sprintf function was just programmed to use the first parameter as the string to use and any others that are specified as replacements.

This is the code in the function:

function sprintf($string)
{
	$arg_list = func_get_args();
	$num_args = count($arg_list);
	
	for($i = 1; $i < $num_args; $i++)
	{
		$string = str_replace('{'.$i.'}', $arg_list[$i], $string);
	}
		
	return $string;
}

So, $string, which is passed, will contain the language string we want to use as you said. func_get_args() then returns an array of all of the paramaters that were passed, which is useful as you can have a function that accepts any number of paramaters and you can still access them. With the example I had above, func_get_args() would return this array:

Array
(
    [0] => Hello, my name is {1} and I am {2}
    [1] => Matt
    [2] => 20
)

Then, because PHP arrays are 0 based (the 1st key is 0, 2nd is 1), we can loop through the array, starting at 1, and perform the replacements in the string; we don't need the value with a key of 0 because we already have that, in $string. Then, looping through the array, the value in the array with a key of 1 (Matt) will replace the {1} in the string, and the value in the array with the key of 2 (20) will replace the {2} in the string.