Actions

JURI/toString

From Joomla! Documentation

Returns a string representation of the URI object. By default the full URI is string is returned, but which parts of the URI are to be returned can be customised with an argument. Note that if the URI is to be output to the user then it should be passed through the JRoute::_ static method first as this will ensure that it is SEF encoded. Note also that the query part separator is "&" rather than "&" and so you will need to pass the output through htmlspecialchars to produce an XHTML-compliant URI.

Contents

Syntax

string toString( $parts )

where:

Argument Data type Description Default
$parts array Associative array of the parts of the URI that are to be rendered. If omitted, then the whole URI is rendered. array( 'scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment' )

The parts are as follows:

Name Description Set method
fragment Fragment. setFragment
host Host name or IP address. setHost
path Path. setPath
pass Password for authentication purposes. setPass
port Port number. setPort
query Query part, consisting of one or more query items. setQuery
scheme Scheme (protocol). setScheme
user User name for authentication purposes. setUser

Example 1

In this example, a URI object is output as a full URI string.

$uri = 'http://fredbloggs:itsasecret@www.example.com:8080/path/to/Joomla/index.php?task=view&id=32#anchorthis';
$u =& JURI::getInstance( $uri );
echo 'URI is ' . $u->toString();

would output

URI is http://fredbloggs:itsasecret@www.example.com:8080/path/to/Joomla/index.php?task=view&id=32#anchorthis

Example 2

In this example, only the scheme, host and path are included.

$uri = 'http://fredbloggs:itsasecret@www.example.com:8080/path/to/Joomla/index.php?task=view&id=32#anchorthis';
$u =& JURI::getInstance( $uri );
echo 'URI is ' . $u->toString( array( 'scheme', 'host', 'path' ) );

would output

URI is http://www.example.com/path/to/Joomla/index.php

Example 3

To produce an XHTML-compliant URI you need to pass the output through htmlspecialchars, like this:

$u =& JURI::getInstance( $uri );
echo 'XHTML-compliant URI is ' . htmlspecialchars( $u->toString() );

See also