Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP preg_replace

Posted on 2010-11-16
5
Medium Priority
?
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 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