- var_dump: documentation ( source)
- strpos: documentation ( source)
- explode: documentation ( source)
- parse_url: documentation ( source)
<?php
$urls = [
'http://evil.com?example.org/foo/bar',
'http://evil.com?example.org/foo/bar?baz#quux',
'http://evil.com?example.org?baz#quux',
'http://evil.com?example.org#quux',
];
foreach ( $urls as $url ) {
$bits = parse_url( $url );
if ( isset( $bits['host'] ) && strpos( $bits['host'], '?' ) !== false ) {
list( $host, $query ) = explode( '?', $bits['host'], 2 );
$bits['host'] = $host;
$bits['query'] = $query
. ( isset( $bits['path'] ) ? $bits['path'] : '' )
. ( isset( $bits['query'] ) ? '?' . $bits['query'] : '' );
unset( $bits['path'] );
}
var_dump( $bits );
}