DATA Methods

encode()

Encodes text in various ways. Use decode() to reverse encoding.

string encode( string text [, int type = 1 ])

Return

Encoded text.

Parameter
Description
textThe text to be encoded.
type0 = no encoding
1 = html-entities QUOTES
2 = html-entities NOQUOTES
3 = url-encode
4 = json-encode
5 = javascript encode

Javascript-encoding will escape quotes and double-qoutes as well as remove any linefeeds or carriage returns. This way you can use the encoded value inside a javascript variable. Make sure to use your JS quotes adequately.

Encode entities

$DATA->encode("<script>alert('test')</script>")
RETURN: '&lt;script&gt;alert(&#039;test&#039;)&lt;/script&gt;'

Encode entities but no quotes

$DATA->encode("<script>alert('test')</script>", 2)
RETURN: '&lt;script&gt;alert('test')&lt;/script&gt;'

URL encode

$DATA->encode("<script>alert('test')</script>", 3)
RETURN: '%3Cscript%3Ealert%28%27test%27%29%3C%2Fscript%3E'

JSON encode

$DATA->encode("alert('test1');" .'alert("test2");', 4)
RETURN: '"alert('test1');alert(\"test2\");"'

Javascript Encode

'<p class="test">' .$DATA->encode('This "code" can go into a ' ."\n" ."into a 'js variable' !!", 5). '</p>'
RETURN: '<p class="test">This &quot;code&quot; can go into a into a &lt;js variable&lt; !!</p>'