gibigbig Posted September 5, 2010 Share Posted September 5, 2010 Ok my input is an array. $episode_number = array('2','99'...'004'); where "..." is any amount of values. My aim is to take these values and format them so that they ALWAYS have 3 characters in them by adding Zero's to the beginning. So 2 will become 002 and 99 will become 099 and 004 will remain 004. I have attempted my code here: for($i=0; $i<$numberlinks; $i++) { $thecount = strlen($episode_number[i]); if($thecount == '1'){ $episode_number[i] == '00'.$episode_number[i].''; } elseif($thecount == '2'){ $episode_number[i] == '0'.$episode_number[i].''; } else { $episode_number[i] == $episode_number[i]; } echo $episode_number[i].'<br />'; } The above output is 2 <br /> tags. But when i add on osme code to it: for($i=0; $i<$numberlinks; $i++) { $thecount = strlen($episode_number[i]); if($thecount == '1'){ $episode_number[i] == '00'.$episode_number[i].''; } elseif($thecount == '2'){ $episode_number[i] == '0'.$episode_number[i].''; } else { $episode_number[i] == $episode_number[i]; } echo $episode_number[i].'<br />'; } $query2[] = ("INSERT INTO episodes (anime_id,anime, username, userid, episode_name, episode_number, manga_chapter_link, moderated) VALUES ('".$anime_id[$i]."', '".$anime[$i]."', '".$username."', '".$userid."', '".clean($episode_name[$i])."', '".clean($episode_number[$i])."', '".clean($manga_chapter_link[$i])."','1')"); } foreach ($query2 as $query){ mysql_query($query) or die (mysql_error()); echo "<span style='color:green'>Added sucessfully <b>Episode ".++$one."</b> </span><br/>"; } the output is the input, ie, 02 remains 02 and not 002. can anyone help me or give me a method that works? Quote Link to comment https://forums.phpfreaks.com/topic/212613-need-urgent-help-with-a-string-formatter/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 5, 2010 Share Posted September 5, 2010 See - http://php.net/sprintf Use a format specifier of %03d Quote Link to comment https://forums.phpfreaks.com/topic/212613-need-urgent-help-with-a-string-formatter/#findComment-1107605 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.