Avatar of foxymoron7
foxymoron7

asked on 

PHP Regex for mixed quantities of consecutive characters

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.


PHPRegular Expressions

Avatar of undefined
Last Comment
foxymoron7
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

(The pipe character acts as a logical OR)
Avatar of foxymoron7
foxymoron7

ASKER

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.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo