Solved

PHP Regex for mixed quantities of consecutive characters

Posted on 2010-11-16
3
451 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
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.

828 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