Solved

PHP preg_replace

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 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