String.toEncoding
toEncoding($encoding) -> string
Description
Convert the string to the given $encoding
scheme.
To revert the string back, use fromEncoding.
NoteTHT does automatic encoding where necessary (e.g. url-encoding for Url objects. This method is provided for the rare cases when you need to do so manually.
Id | description | example |
---|---|---|
url |
url-encoding/percent-encoding | A%20%26%20B |
html |
html entities (special chars only) | A & B |
htmlAll |
html entities (all characters) | A & B |
base64 |
alphanumeric binary-to-text | QSAmIEI= |
punycode |
unicode domain to ascii | xn--mnchen-3ya.de |
(other) | See supported encodings. | — |
$plain = 'Why "THT"?' $plain.toEncoding('url') //= 'Why%20%22THT%22%3F' $plain.toEncoding('html') //= 'Why "THT"?' $plain.toEncoding('htmlAll') //= 'Why "THT"?' $plain.toEncoding('base64') //= 'V2h5ICJUSFQiPw==' $plain.toEncoding('Windows-1251') //= 'Why "THT"?' 'münchen.de'.toEncoding('punycode') //= 'xn--mnchen-3ya.de'
Tip: Avoid Double-EncodingAlways encode a string as late as possible before it is output, to limit the chance of accidentally encoding it twice.