We help IT Professionals succeed at work.

PHP preg_replace

SheppardDigital
on
451 Views
Last Modified: 2012-05-10
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
Comment
Watch Question

Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Try this:

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

Cheers
CERTIFIED EXPERT

Commented:

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

Open in new window

Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Ooops, I was confused! :-(
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
$string = preg_replace("/[^a-zA-Z0-9s\-]/", "", $string);

Open in new window

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.