Solved

preg_replace

Posted on 2006-07-21
4
680 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
[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
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

734 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