?
Solved

PHP Regex for mixed quantities of consecutive characters

Posted on 2010-11-16
3
Medium Priority
?
461 Views
Last Modified: 2012-08-14
Hi all,

I want to match varying quantities of consecutive characters in a string.  
For example, in $string I want to replace with nothing (i.e. ""):
any number of instances (consecutive or not) of !@#
and replace instances of 2 or more consecutive -
and replace instances of 3 or more consecutive +

Example:
$string='d+++og! + ca#t@ - how about the m--ule';
 I'd like to do something like:
echo preg_replace("/[!@#(\-{2,})(\+{3,})]/","",$string);
to get "dog + cat - how about the mule"

Every variant of moving/removing the parentheses and brackets that I've tried replaces either all instances of - and + including where they only occur once, or replaces no instances at all.

Thank you in advance.


0
Comment
Question by:foxymoron7
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 34151487
echo preg_replace("/[!@#]|-{2,}|\+{3,}/","",$string);
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34151494
(The pipe character acts as a logical OR)
0
 
LVL 1

Author Closing Comment

by:foxymoron7
ID: 34151596
Thank you TerryAtOpus!  I'm glad I forgot to mention that I had also tried using the | with no success.  Obviously, I screwed something up when I tried it before.  You are a champion.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

864 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