?
Solved

PHP: Remove duplicate slashes

Posted on 2011-03-16
7
Medium Priority
?
668 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
[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
  • 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
Technology Partners: 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 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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