Solved

PHP preg_replace

Posted on 2010-11-16
5
389 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 500 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php convert date format 3 21
send post values 7 26
PHP curl issue VERBOSE output 18 42
Display images from mysql blob type (Not working) 9 30
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

772 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