Solved

Getting rid of duplicate punctuation

Posted on 2006-11-30
3
229 Views
Last Modified: 2006-11-30
Hello,

I want to remove all duplicates of spaces, dashes, periods and commas.

So
                   Hello--               this is a test,, and only a test........................

Should become
                   Hello- this is a test, and only a test.
0
Comment
Question by:hankknight
  • 3
3 Comments
 
LVL 13

Expert Comment

by:rhickmott
ID: 18047820
$my_string     =       str_replace(array(",,","  ","--",".."), array(","," ","-","."), $my_string);
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18047848
Sorry that should be

$my_string     =       preg_replace(array("/,+/","/ +/","/-+/","/\.+/"), array(","," ","-","."), $my_string);
0
 
LVL 13

Accepted Solution

by:
rhickmott earned 500 total points
ID: 18047897
                   $my_string    =    "                   Hello--               this is a test,, and only a test........................";
                    $my_string    =    preg_replace(array("/,+/","/ +/","/-+/","/\.+/"), array(","," ","-","."), $my_string);
                   
                    // Outputs on php 5.2.0
                    Hello- this is a test, and only a test.

( Sorry to spam your question :) )
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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…
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.

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

11 Experts available now in Live!

Get 1:1 Help Now