Forum Moderators: coopster

Message Too Old, No Replies

Solve this Regular expressions Puzzle!

         

duckxtales

1:41 am on Dec 8, 2006 (gmt 0)

10+ Year Member



$value = preg_replace("/[^\w\s]/","",$value);

That takes out all charaters that aren't alphanumeric, but I want to keep the @ and . symbols. What do I need to add to thsi regular expression to add an "except" cause.

bcc1234

2:06 am on Dec 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just use [^A-Za-z0-9@.] instead.

eelixduppy

3:54 am on Dec 8, 2006 (gmt 0)



>>>Just use [^A-Za-z0-9@.] instead.

If you are going to go that route you need to escape the period like so:


[^A-Za-z0-9@\.\s]

You can also use something like this:


[^\w@\.\s]

Good luck :)

coopster

4:09 pm on Dec 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, you don't have to escape the period inside a character class.

Part of a pattern that is in square brackets is called a "character class". In a character class the only meta-characters are:

\
general escape character
^
negate the class, but only if the first character
-
indicates character range
]
terminates the character class

Resource: Pattern Syntax [php.net]

However, it doesn't hurt to escape it.

eelixduppy

3:26 am on Dec 9, 2006 (gmt 0)



Thanks coop for clarifying that. Assuming is a dangerous thing :)