[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP preg_replace

Posted on 2010-11-16
5
Medium Priority
?
428 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
0
Comment
Question by:SheppardDigital
5 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 34144520
Try this:

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

Cheers
0
 
LVL 14

Expert Comment

by:sam2912
ID: 34144529

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

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 34144544
Ooops, I was confused! :-(
0
 
LVL 13

Accepted Solution

by:
dsmile earned 2000 total points
ID: 34144605
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
 

Expert Comment

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

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

830 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