need help with removing periods [SOLVED]

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

need help with removing periods [SOLVED]

Post 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.
Last edited by s.dot on Thu Nov 10, 2005 4:01 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

that, my friend, is the epitome of awesomeness.

Thanks :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply