Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 634
Decent Dabbler Avatar asked Dec 07 '25 02:12

Decent Dabbler


1 Answers

Just a quick thought:

preg_replace ('#^((http[s]{0,1}://)|([a-z]+://))#i', '$2', $uri);
like image 81
vbence Avatar answered Dec 08 '25 15:12

vbence