Solved

Getting rid of duplicate punctuation

Posted on 2006-11-30
3
230 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 …

867 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

24 Experts available now in Live!

Get 1:1 Help Now