Core\Uri::encodeComponent
Escapes a text so that you can put it into one part of a link, such as a path segment, a fragment or a query value.
Some characters have a meaning in a link. / starts a new part of the path, ? starts the query and & starts a new query pair. Core\Uri::encodeComponent writes each of these as a % and two hex digits. It does the same for every other character except letters, digits and -_.~. Core\Uri::encodeComponent("red shoes & bags") returns red%20shoes%20%26%20bags. A character such as é is two bytes, so it becomes two escapes.
A text that is exactly . or .. throws a LogicError. In a path, such a segment changes which resource the link names. For a query value, use Core\Uri::withQueryParameter or Core\Uri::buildQuery.
Core\Uri::decodeComponent reads the text back. For a value in a form, use Core\Uri::encodeFormValue, which writes a space as +.
The examples below link to a heading, show which characters are escaped, and build download links for uploaded files.
Parameters
$sstringThe text to encode.
Return value
Returns string. The escaped text, with upper-case hex digits. A tainted argument (text from outside the program, such as a request) gives a plain result. No byte of the result can change the structure of the URI.
Errors
LogicError$sis.or... In a path, these move to the same folder or the parent folder. For a value in a query string, use$uri->withQueryParameterorCore\Uri::buildQuery.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Uri::encodeComponent throws a LogicError when its argument is exactly . or ... In a path, such a segment changes which resource the URI names. |
Examples
A link to a heading
<?nvs// Shows how to build a link to a heading on a page, when the heading contains spaces.
string $heading = 'Getting started';
// `Core\Uri::encodeComponent` escapes each character a link cannot contain.// A space becomes `%20`, so this prints /help#Getting%20started.echo '/help#', Core\Uri::encodeComponent($heading), "\n";/help#Getting%20startedCharacters that have a meaning in a link
<?nvs// Shows that the characters with a meaning in a link are escaped, so a value cannot change the link.
// A `/` in a name would start a new part of the path. It becomes `%2F`.// This prints /tags/red%2Fblue.echo '/tags/', Core\Uri::encodeComponent('red/blue'), "\n";
// A `&` would start a new query pair, and `=` would end a name. They become `%26` and `%3D`.// This prints /search?q=salt%20%26%20pepper%3D1.echo '/search?q=', Core\Uri::encodeComponent('salt & pepper=1'), "\n";
// Letters, digits and `-_.~` are not changed.// This prints version-2_final.txt~.echo Core\Uri::encodeComponent('version-2_final.txt~'), "\n";/tags/red%2Fblue/search?q=salt%20%26%20pepper%3D1version-2_final.txt~A download link for an uploaded file
<?nvs// Shows how a server builds the download link for a file a user uploaded.
// In a real program, these names come from the uploaded files.array<string> $files = ['Café menu.pdf', 'Größe (neu).xlsx'];
foreach ($files as string $file) { // Each byte of "é", "ö" and "ß" becomes its own escape, such as `%C3%A9`. echo 'https://example.com/files/', Core\Uri::encodeComponent($file), "\n";}https://example.com/files/Caf%C3%A9%20menu.pdfhttps://example.com/files/Gr%C3%B6%C3%9Fe%20%28neu%29.xlsx