Solved

Regex: remove duplicate slashes [PHP]

Posted on 2009-04-02
5
1,105 Views
Last Modified: 2012-05-06
I want to get rid of all duplicate slashes, for example:

Hello // world ////, how are/// you?

Should become:

Hello / world /, how are/ you?
0
Comment
Question by:level9wizard
5 Comments
 
LVL 15

Expert Comment

by:yuk99
ID: 24050924
s/\/+//
0
 
LVL 13

Assisted Solution

by:marchent
marchent earned 130 total points
ID: 24050927

$str = "Hello // world ////, how are/// you?";

$str = preg_replace('|/+|', '/', $str);

print "$str\n";

Open in new window

0
 
LVL 15

Assisted Solution

by:yuk99
yuk99 earned 20 total points
ID: 24050937
Sorry. s/\/+/\//
0
 
LVL 15

Accepted Solution

by:
fsze88 earned 300 total points
ID: 24051004
<?
$str = 'Hello // world ////, how are/// you?';

$pattern = '/\/+/';
$replacement = '/';
echo preg_replace($pattern, $replacement, $str);

?>
0
 
LVL 3

Assisted Solution

by:FirestormX
FirestormX earned 50 total points
ID: 24053186
As a quick explanation to what everyone's written above, PHP's regex patterns are simply strings, with a / at the begining and end of the pattern, which is why they're putting a \ in front of the /, to "escape" the character. So baiscally /\/+\//, actually becomes a pattern of /+/

Then + sign means to look for 1 or more instance of the previous character, which in this case, is /. It then checks to see if after it finds as many of / as possible, to make sure the last character is /.  
So translated, it means "find one or more /, that ends in /, and replace everything it finds with a single /".
0

Featured Post

Highfive Gives IT Their Time Back

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

706 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

13 Experts available now in Live!

Get 1:1 Help Now