Hello,
I am trying to get a string and then replace each {NO} where NO is a number
For example the string I am replacing
You have {0} steps left {1} {2} {3}
The function I am parsing it through
public static function parse($string, $array = array())
{
global $lang;
#Explode lang category and phrase
$string = explode(':', $string);
#Get phrase from file
$string_rep = $lang[$string[0]][$string[1]];
for($i = 0; $i < count($array); $i++)
{
$string_final = str_replace('{'.$i.'}', $array[$i], $string_rep);
}
return $string_final;
}
The PHP echo
echo Lang::parse('quest:steps_left', array('3', '2', '3'));
What is turns out to be
You have {0} steps left {1} 3 {3}
Any help? Simply it needs to replace all {NUMBERS} with the array so {0} would be 3, {1} would be 2. It only replaces the last number in the array :/
Thanks in advance
if in MyBB, use $updated = $lang->sprintf($string, $arg1, $arg2...)
where $string uses {1}, {2} placeholders
otherwise use $updated = sprintf($string, $arg1, $arg2....) where $string uses %1, %2 placeholders and you can format them as well.
(2012-11-06, 03:28 PM)pavemen Wrote: [ -> ]if in MyBB, use $updated = $lang->sprintf($string, $arg1, $arg2...)
where $string uses {1}, {2} placeholders
otherwise use $updated = sprintf($string, $arg1, $arg2....) where $string uses %1, %2 placeholders and you can format them as well.
I'm writing something myself not using MyBB
Here's what MyBB has:
http://crossreference.mybboard.de/inc/cl....html#l166
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;
}
In your language file, you write something like Number {1}, say, then in your plugin you should first load your language file:
$lang-load("yourlangfilename");
(make sure to define $lang in globals)
then, for example, the variable/numeric value that you want to fetch from a function/string is in a variable called $var, then you can display it by doing something like this:
$var = $mybb->user['uid'] //for example purpose, we take value of a user's uid
//Next, display it
$lang->my_plugin_error = $lang->sprintf($lang->my_plugin_error, $var);
Ah, disregard, I saw now you need this outside of MyBB, follow Paul's post then.
(2012-11-06, 04:04 PM)Paul H. Wrote: [ -> ]Here's what MyBB has:
http://crossreference.mybboard.de/inc/cl....html#l166
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;
}
Nope it's still not working :s, still only parsing the last result.
(2012-11-06, 04:06 PM)crazy4cs Wrote: [ -> ]In your language file, you write something like Number {1}, say, then in your plugin you should first load your language file:
$lang-load("yourlangfilename");
(make sure to define $lang in globals)
then, for example, the variable/numeric value that you want to fetch from a function/string is in a variable called $var, then you can display it by doing something like this:
$var = $mybb->user['uid'] //for example purpose, we take value of a user's uid
//Next, display it
$lang->my_plugin_error = $lang->sprintf($lang->my_plugin_error, $var);
Ah, disregard, I saw now you need this outside of MyBB, follow Paul's post then.
Thanks for the indepth post but as you say i'm not using MyBB on this part of the project.
(2012-11-06, 04:15 PM)Exze Wrote: [ -> ] (2012-11-06, 04:04 PM)Paul H. Wrote: [ -> ]Here's what MyBB has:
http://crossreference.mybboard.de/inc/cl....html#l166
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;
}
Nope it's still not working :s, still only parsing the last result.
Can you post the code you're using?
(2012-11-06, 04:17 PM)Paul H. Wrote: [ -> ] (2012-11-06, 04:15 PM)Exze Wrote: [ -> ] (2012-11-06, 04:04 PM)Paul H. Wrote: [ -> ]Here's what MyBB has:
http://crossreference.mybboard.de/inc/cl....html#l166
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;
}
Nope it's still not working :s, still only parsing the last result.
Can you post the code you're using?
I'm using the code in the top post.
Could you post the full lang class please?
<?php
class Lang
{
public static function parse($string, $array = array())
{
global $lang;
#Explode lang category and phrase
$string = explode(':', $string);
#Get phrase from file
$string_rep = $lang[$string[0]][$string[1]];
$string_final = $string_rep;
for($i = 0; $i < count($array); $i++)
{
$string_final = str_replace('{'.$i.'}', $array[$i], $string_rep);
}
return $string_final;
}
}
?>
It's WIP
The en.lang.php contains
#GENERAL
$lang['general']['logged_in'] = 'You are logged in';
$lang['general']['login'] = 'Login';
$lang['general']['register'] = 'Register';
$lang['general']['logged_in_as'] = 'You are {1} logged in as {0}';
To parse it I am using
echo Lang::parse('general:logged_in_as', array(User::username(), '3'));
As you can see when it parses it, it includes the users language.lang file then general:logged_in_as is split up and gets called, the array should then be replaced in the string as positions indicated.
It should print out
You are 3 logged in as Ian
The most I have it to show is
You are 3 logged in as {0}
UPDATE 16:35pm