Search code examples
phpregexcapturenegative-lookbehind

Regex: remove scheme unless it's http(s). (capture negative lookbehind pattern)


I'm having a regex blackout here. How do I capture a negative lookbehind pattern again?

I'm trying to remove the scheme (including ://) of a uri unless it is http/https. I'm half way there (or I thought I was, the pattern below doesn't even compile), but I forgot how to actually capture the negative pattern:

preg_replace( '~^(?<!https?)://~', '', $uri );

How can I do this, again?


Solution

  • preg_replace('#^((?:.(?<!http))+://)#i', '', $uri);