value) (obtained with
* the function parse_str()).
*
* @param array(string=>mixed) $parts The parts of the URL
* @return string
*/
public static function buildUrl( array $parts )
{
$path = ( isset( $parts['path'] ) ) ? $parts['path'] : '/';
$query = ( isset( $parts['query'] ) ) ? '?' . http_build_query( $parts['query'] ) : '';
$fragment = ( isset( $parts['fragment'] ) ) ? '#' . $parts['fragment'] : '';
if ( isset( $parts['host'] ) )
{
$host = $parts['host'];
$scheme = ( isset( $parts['scheme'] ) ) ? $parts['scheme'] . '://' : 'http://';
$port = ( isset( $parts['port'] ) ) ? ':' . $parts['port'] : '';
$result = "{$scheme}{$host}{$port}{$path}{$query}{$fragment}";
}
else
{
$result = "{$path}{$query}{$fragment}";
}
return $result;
}
/**
* Parses the provided string and returns an associative array structure.
*
* It implements the functionality of the PHP function parse_str(), but
* without converting dots to underscores in parameter names.
*
* Example:
*
* $str = 'foo[]=bar&openid.nonce=123456';
*
* parse_str( $str, $params );
* $params = ezcUrlTools::parseQuery( $str );
*
*
* In the first case (parse_str()), $params will be:
*
* array( 'foo' => array( 'bar' ), 'openid_nonce' => '123456' );
*
*
* In the second case (ezcUrlTools::parseQueryString()), $params will be:
*
* array( 'foo' => array( 'bar' ), 'openid.nonce' => '123456' );
*
*
* The same function is defined in {@link ezcUrlTools} in the Url component.
*
* @param array(string=>mixed) $str The string to parse
* @return array(string=>mixed)
*/
public static function parseQueryString( $str )
{
$result = array();
// $params will be returned, but first we have to ensure that the dots
// are not converted to underscores
parse_str( $str, $params );
$separator = ini_get( 'arg_separator.input' );
if ( empty( $separator ) )
{
$separator = '&';
}
// go through $params and ensure that the dots are not converted to underscores
$args = explode( $separator, $str );
foreach ( $args as $arg )
{
$parts = explode( '=', $arg, 2 );
if ( !isset( $parts[1] ) )
{
$parts[1] = null;
}
if ( substr_count( $parts[0], '[' ) === 0 )
{
$key = $parts[0];
}
else
{
$key = substr( $parts[0], 0, strpos( $parts[0], '[' ) );
}
$paramKey = str_replace( '.', '_', $key );
if ( isset( $params[$paramKey] ) && strpos( $paramKey, '_' ) !== false )
{
$newKey = '';
for ( $i = 0; $i < strlen( $paramKey ); $i++ )
{
$newKey .= ( $paramKey{$i} === '_' && $key{$i} === '.' ) ? '.' : $paramKey{$i};
}
$keys = array_keys( $params );
if ( ( $pos = array_search( $paramKey, $keys ) ) !== false )
{
$keys[$pos] = $newKey;
}
$values = array_values( $params );
$params = array_combine( $keys, $values );
}
}
return $params;
}
}
?>