Solved

PHP/REGEX: Remove periods and duplicate slashes

Posted on 2011-03-16
6
812 Views
Last Modified: 2012-05-11
The following code works exactly the way I want it to, however I would rather the last two lines be combined into one regular expression.

First, all periods should be removed.  Then, all slashes and groups of slashes should be converted to a single slash (DIRECTORY_SEPARATOR)

All slashes should be converted to the DIRECTORY_SEPARATOR.

So, on Windows, this:
..///my\\\\..///sub\\dir/

Should become this:
\my\sub\dir\

And on Linux, this:
..///my\\\\..///sub\\dir/
Should become this:
/my/sub/dir/

<?php

$source = "..///my\\\\\\\\..///sub\\\\dir/";

// The following code works exactly the way I want it to, however I would rather the last two lines be combined into one regular expression.
$source = str_replace('.','',$source);
$result = preg_replace('#[\\\\/]+#', DIRECTORY_SEPARATOR, $source);

echo $result;

?>

Open in new window

0
Comment
Question by:hankknight
  • 4
  • 2
6 Comments
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 350 total points
ID: 35153118
My first thought is: Why? If it works, and runs quickly, and is understandable (and thus maintainable), then you don't really have a problem to solve!

However, it may not be safe to be removing all periods, as it can be part of a directory name.

This appears to work, and allows periods in directory names to remain:

$result = preg_replace('#(^|[/\\]*)\.\.[/\\]+|[\\/]+#', DIRECTORY_SEPARATOR, $source);

Open in new window

0
 
LVL 7

Assisted Solution

by:lexlythius
lexlythius earned 150 total points
ID: 35153123
<?php

$source = "..///my\\\\\\\\..///sub\\\\dir/";

$result = preg_replace(array('/\.+/', '#[\\\\/]+#'), array('', DIRECTORY_SEPARATOR), $source);

echo $result;

?>
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35153124
Even so, if your code may be maintained by someone else in the future, it would be clearer to separate it into 2 replaces.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 7

Expert Comment

by:lexlythius
ID: 35153128
Note that preg_replace accepts arrays as parameters to do succesive replacements.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35153139
Minor correction, to allow for directory names like dir.. :
$result = preg_replace('#(^|[/\\]+)\.\.[/\\]+|[\\/]+#', DIRECTORY_SEPARATOR, $source);
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 350 total points
ID: 35153167
My recommended approach for maintainability:

$result = preg_replace('#[\\/]+#', DIRECTORY_SEPARATOR, $source); #remove excess separators
$result = preg_replace('#(^|[\\/])\.\.?[\\/]#', DIRECTORY_SEPARATOR, $result); #remove references to the special cases .. and . as directories

Things that might still trip you up (but probably won't matter):
1. Removing a .. from part of a path may of course make the resulting path invalid
2. linux paths can also have / and \ as part of the directory name - it just needs to be escaped with a \ character
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question