Jump to content

Var Replacement


exze

Recommended Posts

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())
{
if(empty($array))
{
return $output;
}

for($i=0;$i<count($array);$i++)
{
$output = str_replace('{'.$i.'}', $array[$i], $string);
}

return $output;
}

 

The PHP echo

 

 echo Lang::parse($lang['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 other solutions to make the function smaller will also be appreciated

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/270359-var-replacement/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.