• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

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
0
SheppardDigital
Asked:
SheppardDigital
1 Solution
 
Marco GasiFreelancerCommented:
Try this:

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

Cheers
0
 
Samuel LiewCommented:

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

Open in new window

0
 
Marco GasiFreelancerCommented:
Ooops, I was confused! :-(
0
 
dsmileCommented:
I think there's no needs to escape - in that expression.

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

is enough.


@SheppardDigital: here's a nice tutorrial about regular expression which might help you in the future http://articles.sitepoint.com/article/regular-expressions-php
0
 
david_coleman_007Commented:
$string = preg_replace("/[^a-zA-Z0-9s\-]/", "", $string);

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now