preg_replace, exact opposite of a preg_match

Posted by SoLoGHoST on Stack Overflow See other posts from Stack Overflow or by SoLoGHoST
Published on 2010-05-03T17:26:52Z Indexed on 2010/05/03 17:48 UTC
Read the original article Hit count: 441

Filed under:
|
|
|

I need to do a preg_replace for the exact opposite of this preg_match regular expression:

preg_match('#^(\w+/){0,2}\w+\.\w+$#', $string);

So I need to replace all strings that are not valid with an empty string -> ''

So it needs to remove the first / and last / if found, and all non-valid characters, that is the only valid characters are A-Z, a-z, 0-9, _, ., and / (if it's not the first or last characters of the string).

How can I accomplish this with the preg_replace?

Thanks :)

© Stack Overflow or respective owner

Related posts about preg-replace

Related posts about preg-match