Jump to content

Need urgent help with a string formatter.


gibigbig

Recommended Posts

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.