Avatar of SheppardDigital
SheppardDigital
 asked on

PHP preg_replace

Hi,

I currently have this line of code which cleans a string. Could someone tell me how I would change this to allow the minus sign (-)?

$string = preg_replace("/[^a-zA-Z0-9s]/", "", $string);

Thanks
PHP

Avatar of undefined
Last Comment
david_coleman_007

8/22/2022 - Mon
Marco Gasi

Try this:

$string = preg_replace("/[^a-zA-Z0-9s][^-]/", "", $string);

Cheers
Samuel Liew


$string = preg_replace("/[^a-zA-Z0-9s\-]/", "", $string);

Open in new window

Marco Gasi

Ooops, I was confused! :-(
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
dsmile

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
david_coleman_007

$string = preg_replace("/[^a-zA-Z0-9s\-]/", "", $string);

Open in new window