Solved

Regex: remove duplicate slashes [PHP]

Posted on 2009-04-02
5
1,116 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
[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
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

729 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