preg_replace, exact opposite of a preg_match
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 fi开发者_StackOverflowrst or last characters of the string).
How can I accomplish this with the preg_replace?
Thanks :)
preg_replace('#^/|/$|[^A-Za-z0-9_./]#D', '', $subject);
精彩评论