Solved

PHP: Remove duplicate slashes

Posted on 2011-03-16
7
661 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

770 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