exze Posted November 6, 2012 Share Posted November 6, 2012 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 More sharing options...
exze Posted November 6, 2012 Author Share Posted November 6, 2012 It still only seems to replace the last var for some reason :s Link to comment https://forums.phpfreaks.com/topic/270359-var-replacement/#findComment-1390549 Share on other sites More sharing options...
Jessica Posted November 7, 2012 Share Posted November 7, 2012 Every time you set $output you're using $string in the replace function. So the last time it sets $output = the string replacing the last one only. Link to comment https://forums.phpfreaks.com/topic/270359-var-replacement/#findComment-1390666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.