php - How to dynamically update a file name with increasing integers? -



php - How to dynamically update a file name with increasing integers? -

i want update file name looks each time updated.

some_name_1, some_name_2, some_name_3, etc.

each update increment trailing integer one. below prototype, wanted create sure best practice way it, before implementing it. also, i'm not 100% php implicit casting correctly.

is practice way update file name?

// ... in class private function updatefilename() { $pattern = '#_(\d+)$#'; $subject = $this->file_name; // $subject holds current file name preg_match($pattern, $subject, $matches); if($matches[0]) { $patterns = array(); $temp = $matches[0]; $patterns[0] = '/$temp$/'; $replacements = array(); $replacements[0] = $matches[0] + 1; preg_replace($patterns, $replacements, $subject); // $subject holds new name } }

clarification

the file name hash, , 1 string of characters. the _1, etc @ end of string ( perhaps utilize anchor )

you can whole thing in 1 preg_replace:

preg_replace("/\b([^\w]+)\b/i",'_',$filename);

php

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -