Name

htmlencode — convert special characters to HTML entities

Synopsis

string htmlencode(html);
string html;

Description

Scans the string html for characters that commonly interfere with HTML markup and converts them into their corresponding entites. The return value is a new string with the conversions made.

This function is useful for converting user supplied text into a form which is suitable for display on an HTML page.

The following conversions are made:

  • "&" (ampersand) becomes "&"
  • "\"" (double quote) becomes """
  • "\'" (single quote) becomes "'"
  • "<" (less than) becomes "&lt;"
  • ">" (greater than) becomes "&gt;"
  • Any character with an ASCII value greater than or equal to 128 becomes "&#xxx;" where xxx is the ASCII value of the character.

Example 37. htmlencode Example

The RSP code:

print(htmlencode("The paragraph tag: <P>\n"));

would output the result:

The paragraph tag: &lt;P&gt;