Solved

PHP/REGEX: Remove periods and duplicate slashes

Posted on 2011-03-16
6
814 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to use a switch statement with heredoc 11 33
Why is my wamp get_include_path() wrong? 2 21
Custom Wordpress Loop 22 38
How to delete values from an array using php 4 22
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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.

856 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