Actions

Difference between revisions of "JURI/toString"

From Joomla! Documentation

(New page: 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 t...)
 
m (See also: re-categorisation)
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
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/(underscore)|JRoute::_]] static method first as this will ensure that it is SEF encoded.
+
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/(underscore)|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 [[php:htmlspecialchars|htmlspecialchars]] to produce an XHTML-compliant URI.
 
===Syntax===
 
===Syntax===
 
string toString( $parts )
 
string toString( $parts )
Line 73: Line 73:
 
would output
 
would output
 
  URI is http://www.example.com/path/to/Joomla/index.php
 
  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 [[php:htmlspecialchars|htmlspecialchars]], like this:
 +
<source lang="php">
 +
$u =& JURI::getInstance( $uri );
 +
echo 'XHTML-compliant URI is ' . htmlspecialchars( $u->toString() );
 +
</source>
 +
 
===See also===
 
===See also===
 
* [http://api.joomla.org/Joomla-Framework/Environment/JURI.html#toString JURI->toString on api.joomla.org]
 
* [http://api.joomla.org/Joomla-Framework/Environment/JURI.html#toString JURI->toString on api.joomla.org]
 
* [[JRoute/(underscore)|JRoute::_]]
 
* [[JRoute/(underscore)|JRoute::_]]
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JURI]]</noinclude>
+
<noinclude>[[Category:JURI]]</noinclude>

Latest revision as of 12:43, 9 August 2012

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 "&amp;" 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