Solved

Getting rid of duplicate punctuation

Posted on 2006-11-30
3
233 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to count occurrences of each item in an array.

740 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