Skip to content

Instantly share code, notes, and snippets.

@boekkooi
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save boekkooi/d24fcee6f7082e802ce1 to your computer and use it in GitHub Desktop.
Save boekkooi/d24fcee6f7082e802ce1 to your computer and use it in GitHub Desktop.
PHP RFC3986 regex
<?php
const PATTERN_RFC3986 = '~^
(?<scheme>[a-z][a-z0-9+-\.]*)
://
(?:
(?<userinfo>
([a-z0-9\-\._\~!\$&\'()*+,;=:]|%[0-9a-f]{2})*
)@ # *( unreserved / pct-encoded / sub-delims / ":" )
)?
(?<host>
(?<ipv4>
(?<ipv4Dec>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
(?:\.(?P>ipv4Dec)){3}
) # a IPv4 address
|
(?<ipv6>
\[
(?:
(?:
(?<ipv6h16>[0-9a-f]{1,4}):(?:(?P>ipv6h16):){5}
(?<ipv6ls32>
(?:(?P>ipv6h16):(?P>ipv6h16))
|
(?<ipv6ipv4>
(?<ipv6ipv4Dec>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
(?:\.(?P>ipv6ipv4Dec)){3}
)
)
) # 6( h16 ":" ) ls32
|
(?:::(?:(?P>ipv6h16):){5}(?P>ipv6ls32)) # "::" 5( h16 ":" ) ls32
|
(?:(?P>ipv6h16)?::(?:(?P>ipv6h16):){4}(?P>ipv6ls32)) # [ h16 ] "::" 4( h16 ":" ) ls32
|
(?:(?:(?:(?P>ipv6h16):){1,2}|:):(?:(?P>ipv6h16):){3}(?P>ipv6ls32)) # [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
|
(?:(?:(?:(?P>ipv6h16):){1,3}|:):(?:(?P>ipv6h16):){2}(?P>ipv6ls32)) # [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
|
(?:(?:(?:(?P>ipv6h16):){1,4}|:):(?P>ipv6h16):(?P>ipv6ls32)) # [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
|
(?:(?:(?:(?P>ipv6h16):){1,5}|:):(?P>ipv6ls32)) # [ *4( h16 ":" ) h16 ] "::" ls32
|
(?:(?:(?:(?P>ipv6h16):){1,6}|:):(?P>ipv6h16)) # [ *5( h16 ":" ) h16 ] "::" h16
|
(?:(?:(?:(?P>ipv6h16):){1,7}|:):) # [ *6( h16 ":" ) h16 ] "::"
)
\]
) # a IPv6 address
|
(?<ipvfuture>
\[
v[0-9a-f]+ \. [a-z0-9\-\._\~!\$&\'()*+,;=:]+ # "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
\]
) # a future IP address
|
(?<regname>
([a-z0-9\-\._\~!\$&\'()*+,;=]|%[0-9a-f]{2})* # *( unreserved / pct-encoded / sub-delims )
) # a registered name
)
(?:
:(?<port>[0-9]*)
)?
(?<pathabempty>
(?:
/(?<segment>([a-z0-9\-\._\~!\$&\'()*+,;=:@]|%[0-9a-f]{2})*) # *pchar
)*
) # *( "/" segment )
(?:
\?
(?<query>
([a-z0-9\-\._\~!\$&\'()*+,;=:@/?]|%[0-9a-f]{2})*
) # *( pchar / "/" / "?" )
)?
(?:
\#
(?<fragment>
([a-z0-9\-\._\~!\$&\'()*+,;=:@/?]|%[0-9a-f]{2})*
) # *( pchar / "/" / "?" )
)?
$~ixu'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment