Solved

PHP: Remove duplicate slashes

Posted on 2011-03-16
7
663 Views
Last Modified: 2012-05-11
Using PHP, I want to remove all duplicate slashes.

       ////my/sub/////dir/
should become
       /my/sub/dir/

and
       \\\\my\sub\\\\dir
should become
       \my\sub\dir
0
Comment
Question by:hankknight
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35150010
Try this out:
$source = "////my/sub/////dir/";
$result = preg_replace("#/+#", "/", $source);

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 35150058
Thanks, but I want it to work with forward slashes and backward slashes.
<?php

$source = "\\\\my\\\\\\sub\\\\dir\\\\\\";
$result = preg_replace("#/+#", "/", $source);
echo $result;

?>

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35150261
Sorry, modified for both kinds of slashes:
$source = '////my/sub/////dir/';
//$source = '\\\\my\sub\\\\dir';
$result = preg_replace('#([\\\\/])\1+#', "$1", $source);

Open in new window

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 16

Author Comment

by:hankknight
ID: 35150420
Thanks.  Please try this:
<?php

$source = "\\\\//my/////////\\\\sub////\\\\dir\\\\\\1/2\\3\\4";
$result = preg_replace('#([\\\\/])\1+#', DIRECTORY_SEPARATOR, $source);
echo $result;

?>

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35151646
I think we have a discrepancy in the requirement  = )

Should "\\//" be changed into "\", "\/", or "\\"?

Your last post, because of how the patter is constructed, will replace "\\\\" with DIRECTORY_SEPARATOR, and will then replace "//" with DIRECTORY_SEPARATOR, resulting in "\\my...". This doesn't seem like what you are after : )
0
 
LVL 16

Author Comment

by:hankknight
ID: 35151901
Thanks, kaufmed.

I don't want two slashes in a row regardless what kind of slashes they are.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35151934
>>  I don't want two slashes in a row regardless what kind of slashes they are.

Awesome. That's easy to fix  = )

$source = "\\\\//my/////////\\\\sub////\\\\dir\\\\\\1/2\\3\\4";
$result = preg_replace('#[\\\\/]{2,}#', DIRECTORY_SEPARATOR, $source);

Open in new window


And if you want to normalize ALL slashes:
$source = "\\\\//my/////////\\\\sub////\\\\dir\\\\\\1/2\\3\\4";
$result = preg_replace('#[\\\\/]+#', DIRECTORY_SEPARATOR, $source);

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
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…

831 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