Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-01-2004, 06:21 PM   #1
Registered: Feb 2004
Posts: 781

Rep: Reputation: Disabled
PHP-Remove multiple chars


I need a little help for a way to remove multiple characters from a string.
A letter can be written up to 5 times for example, a number 15 times,
but all other characters max 2 times.

For example:
!!!!!!!!!!!! should become !
coooooooooooooooooooool should become coooool

I could make this:
PHP Code:
$text preg_replace"/\!{1,}/""!"$text ); 
to remove multiple "!"

But do I have to write this line for each character? There must be an easier way, maybe several lines but not one for each character.

Thank you for any help!
With regards, Boby!
Old 10-01-2004, 08:34 PM   #2
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
I thought this could be done with a simple pattern, but it turns out that ([ABC]){2,} matches "ACBAAC" and all sorts of crap, and using a backreference doesn't make things any better, as it references the pattern instead of the match.

You could do it by using a loop to build your search and replace strings, though this might be relatively slow, or you could write your own function to run through the input string and keep count of how many times the last character appeared (I think this would be quite easy, and quite a bit faster than applying 60+ regexes in a loop).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
script to remove spaces from multiple filenames jeffreybluml Linux - Newbie 36 07-31-2013 02:10 AM
cyrillic chars - should I upgrade mysql or php ojav Linux - Software 0 05-29-2005 04:46 AM
Multiple instances of PHP cubax Linux - Newbie 6 12-30-2004 08:04 PM
How to remove a php zend extension docbrazen Linux - Software 0 01-19-2004 02:17 PM
How to remove or strip control chars in the 'script' utility? fieldmethods Linux - General 1 10-24-2003 01:11 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration