Page 1 of 1

need help with removing periods [SOLVED]

Posted: Thu Nov 10, 2005 3:12 pm
by s.dot
I need to remove periods (.) from filenames, UNLESS it is the period for the extension.
For example if something was named 1.1.2.3.jpg I would need it to be named 1123.jpg.

I can't think of any logic for this =/ perhaps because my regex knowledge is not too great.

Posted: Thu Nov 10, 2005 3:20 pm
by feyd

Code: Select all

[feyd@work]>php -r "$a = '1.1.2.3.jpg'; function stripP($a) { array_shift($a); $a[0] = str_replace('.','',$a[0]); return implode('',$a); } echo preg_replace_callback('#^(.*?)(\.[^\.]+?)?$#','stripP',$a);"
1123.jpg
tested in PHP 5.1

Posted: Thu Nov 10, 2005 4:01 pm
by s.dot
that, my friend, is the epitome of awesomeness.

Thanks :)