Solved

preg_replace

Posted on 2006-07-21
4
673 Views
Last Modified: 2008-02-26
what do i change so my preg_replace allows spaces too?  At the mo, it allows letters a-z upper and lower and numbers 0-9... but i need to keep spaces...

preg_replace('/[^a-z0-9]/i','',$string);
0
Comment
Question by:ellandrd
  • 2
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 17153111
Add a space into your regexp:

preg_replace('/[^a-z0-9 ]/i','',$string);

-r-
0
 
LVL 16

Author Comment

by:ellandrd
ID: 17153114
oh its as simple as that?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17153123
Yes :-)

The block-anchors define a set of characters [a-z] matches all alpha's, [a-z ] al alpha's and spaces.

By using [^ instead of [ you can match all characters but the ones inside the [].

So [^a-z0-9 ] matches all but alphanumerics and spaces.

-r-
0
 
LVL 16

Author Comment

by:ellandrd
ID: 17153132
Thank you!
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 …

777 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