PHP - Rename pictures sequentially double digit -
I have the following PHP code to rearrange the images in sequential order:
Function sequential images ($ path)) {$ i = 1; $ Files = globe ($ path. "/ {*. Gif, * .jpg, * .jpeg, * .png}", GLOB_BRACE | GLOB_NOSORT); $ Count = count ($ files); Foreign Currency ($ file $ file) {$ newname = str_pad ($ i, strollon ($ count) +1, '0', STR_PAD_LEFT); $ Ext = substr (strrchr ($ file, '.'), 1); $ Newname = $ path '/ picture'. $ Newname '.' $ Ext. If ($ file! = $ Newname) {Rename ($ file, $ newname); } $ i ++; }} I would like to rename
01.jpg to picture01.jpg 02.jpg to picture02.jpg 03.jpg picture03.jpg 04. Picture for JPG 04.jpg ..... 10.jpg picture 10.jpg 11.jpg picture 11.jpg I get the output
< Code> Picture 00.jpg Pictures 003.jpg Pictures 004.jpg .... Photos 101.jpg Photos 011.jpg I like that if the image sequence is less than 9 then It will be a '0' before it, if it is 10 or more then it will not have extra zeros.
I tried to add ($ i = 1; $ i & lt; = $ 21; $ i ++) to
{If ($ i & lt; = 9) {// code} else {// code}} but it also does not work. str_pad and , then you are living in 99 files, then you should harden that length You want to do in the pad. $ newname = str_pad ($ i, 2, '0', STR_PAD_LEFT); Of course, as the Lose Cheron is correctly described, the best way to do this is to use sprintf . sprintf ("% 02d", $ i)
Comments
Post a Comment