Xbasic

HTMLENCODE Function

IN THIS PAGE

Syntax

Encoded_String as C = HTMLENCODE(input as C [, strict as L])

Arguments

inputCharacter

The string to be encoded.

strictLogical

Default = .F.. Indicates whether or not strict encoding should be used. If .F., only the most common characters are substituted with their HTML entity value. If .T., all characters which have HTML character entities will be converted.

Returns

encode_htmlCharacter

Returns the encoded string.

Description

HTMLENCODE() transforms a string in order to make it HTML-safe. Certain characters are have special meanings within HTML documents and should not be used directly.

  • & becomes &
  • " becomes &quote;
  • ' becomes '
  • < becomes &lt;
  • > becomes &gt;

This is sufficient for most all web applications and allows you to easily store HTML and other reserved characters in your database and convert them on-the-fly for display within a web page.

Example 

dim html as c =<<%html%
<div style="width:50%;height:400px;">
— Northwinds Trading Co. 2017 —
</div>
%html%

? htmlencode(html)
= &lt;div style=&quot;width:50%;height:400px;&quot;&gt;
&mdash; Northwinds Trading Co. 2017 &mdash;
&lt;/div&gt;

? htmlencode(html, .f.)
= &lt;div style=&quot;width:50%;height:400px;&quot;&gt;
— Northwinds Trading Co. 2017 —
&lt;/div&gt;

Decoding HTML 

To decode an encoded string, use *html_unescape():

dim encodedHtml as c = htmlencode(html)
? encodedHTML
= &lt;div style=&quot;width:50%;height:400px;&quot;&gt;
— Northwinds Trading Co. 2017 —
&lt;/div&gt;


? *html_unescape(encodedHtml)
= <div style="width:50%;height:400px;">
— Northwinds Trading Co. 2017 —
</div>

See Also