?
Solved

PHP/REGEX: Remove periods and duplicate slashes

Posted on 2011-03-16
6
Medium Priority
?
821 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

771 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