views:

38

answers:

2

Hi, I'm trying everything in a string BUT a certain pattern.

My pattern is:

$pattern = "/^[a-zA-Z0-9]+$/D";

And I want to do the following:

$string = preg_replace(anything but $pattern, "", $string);

How do I do that?

Thanks!

+1  A: 
$pattern = /^[^a-zA-Z0-9]+$/D

The ^ between the square brackets, [ and ], will make the group match all not of them. This will match from beginning of string to end, I'm not sure wether you need that too.

$pattern = "[^a-zA-Z0-9]+?"

Would work matching all the parts of the string which does not get matched by the other pattern.

Pindatjuh
+3  A: 

Use the caret "^" in the character class to say you don't want these characters.

$pattern = "/^[^a-zA-Z0-9]+$/D";

Explanation

^ Negates the character class, causing it to match a single character not listed in the character class. (Specifies a caret if placed anywhere except after the opening [)

Source, regular-expressions.info

The Pixel Developer
Thanks! regex is a hell for a new beginner :/
soren.qvist
Regular expressions are easy, you just need to keep on practicing :-)
The Pixel Developer