Solved

PHP Regex for mixed quantities of consecutive characters

Posted on 2010-11-16
3
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

726 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