Solved

PHP/REGEX: Remove periods and duplicate slashes

Posted on 2011-03-16
6
805 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
Comment Utility
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
Comment Utility
<?php

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

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

echo $result;

?>
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

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

Expert Comment

by:Terry Woods
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now