Please consider using the the latest stable version for any production code.

The Fragment component

The library provides a Fragment class to ease fragment creation and manipulation.


Using the default constructor.


public function __contruct($fragment = null)

The constructor accepts:


use League\Uri\Components\Fragment;

$fragment = new Fragment('eur%20o');
echo $fragment->getContent();      //display 'eur%20o'
echo $fragment;                    //display 'eur%20o'
echo $fragment->getUriComponent(); //display '#eur%20o'

$fragment = new Fragment();
echo $fragment->getContent();      //display null
echo $fragment;                    //display ''
echo $fragment->getUriComponent(); //display ''

$fragment = new Fragment('');
echo $fragment->getContent();      //display ''
echo $fragment;                    //display ''
echo $fragment->getUriComponent(); //display '#'

If the submitted value is not a valid, an InvalidArgumentException exception is thrown.

On instantiation the submitted string is normalized using RFC3986 rules.

Using a League Uri object

You can acces a League\Uri\Components\Fragment object with an already instantiated League Uri object.


use League\Uri\Fragments\Http as HttpUri;

$uri  = HttpUri::createFromString('');
$fragment = $uri->fragment; // $fragment is a League\Uri\Components\Fragment object;


The component representation, comparison and manipulation is done using the package UriPart and the Component interfaces.


New since version 4.2

Returns the decoded value of a fragment component


public Fragment::getDecoded(void): null|string



use League\Uri\Components\Fragment;

$component = new Fragment('%E2%82%AC');
echo $component->getUriComponent(); //displays '#%E2%82%AC'
echo $component->getDecoded(); //displays '€'