?
Solved

PHP: Remove duplicate slashes

Posted on 2011-03-16
7
Medium Priority
?
666 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

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