开发者

preg_replace, exact opposite of a preg_match

开发者 https://www.devze.com 2022-12-28 19:39 出处:网络
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);

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消