Solved

PHP: Remove duplicate slashes

Posted on 2011-03-16
7
659 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 74

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 74

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 74

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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 94
.htaccess file settings 4 36
Link SQL table to Webpage 9 39
PHP loop not working 4 33
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now