[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP/REGEX: Remove periods and duplicate slashes

Posted on 2011-03-16
6
Medium Priority
?
824 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 1400 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 600 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1400 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 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