"string", by mapping characters (or sequences of characters) to an alternative character (or sequence of characters). This macro can be used to encode strings for use in URLs, to encode to HTML entities, to protect quotes, and for as many other uses as you can imagine.
%ENCODE{"string"}%
| Parameter | Description   | Default | 
|---|---|---|
| type="encodingname" | Use a predefined encoding (see below). | Default is 'url'. Parameter typenot be used ifoldorneware given. | 
| "string" | String to encode | "" (empty string) | 
| old="tokenlist" | Comma-separated list of tokens to replace. Tokens are normally single characters, but can also be sequences of characters. The standard format tokens may be used in this list. Each token must be unique - you cannot list the same token twice. | May not be used with type; required ifnewis used | 
| new="tokenlist" | comma-separated list of replacement tokens. The elements in this list match 1:1 with the elements in the oldlist. Again, the standard format tokens may be used. An empty element in thenewlist will result in the corresponding token in theoldlist being deleted from the string. If thenewlist is shorter than theoldlist it will be extended to the same length using the empty element. Tokens do not have to be unique.
 | May not be used with type; required ifoldis used | 
ENCODE is called with no optional parameters (e.g. %ENCODE{"string"}%) then the default type="url" encoding will be used.
type parameter encodes the following "special characters"  "\n") and carriage return ("\r")
"<", ">", "&", single quote (') and double quote (")
"%", "[", "]", "@", "_", "*", "=" and "|"
type="entity" or type="entities" Encode special characters into HTML entities, like a double quote into ". Does not encode \n (newline).
type="html" As type="entity" except it also encodes \n (newline)
type="safe" Encode just the characters '"<>% into HTML entities.
type="quote" or type="quotes" Escapes double quotes with backslashes (\"), does not change any other characters
type="url" Encode special characters for use in URL parameters, like a double quote into %22
   %ENCODE{"spaced name"}%= expands to
      spaced%20name
   %ENCODE{"| Blah | | More blah |" old="|,$n" new="|,<br />"}% expands to
      | Blah | | More blah |
      - this encoding is useful to protect special TML characters in tables.
   %ENCODE{"10xx1x01x" old="1,x,0" new="A,,B"}% expands to
      ABABA
   %ENCODE{"1,2" old="$comma" new=";"}% expands to
      1;2
<input type="text" name="address" value="%ENCODE{ "any text" type="entity" }%" />
ENCODE can be used to filter user input from URL parameters and similar to help protect against cross-site scripting. The safest approach is to use type="entity". This can however prevent an application from fully working. You can alternatively use type="safe" which encodes only the characters '"<>% into HTML entities. When ENCODE is passing a string inside another macro always use double quotes ("") type="quote". For maximum protection against cross-site scripting you are advised to install the Foswiki:Extensions.SafeWikiPlugin.
%SEARCH{ "%ENCODE{ "string with "quotes"" type="quotes" }%" noheader="on" }%   When using
oldandnew, be aware that the results of applying earlier tokens are not processed again using later tokens. For example:%ENCODE{"A" old="A,B" new="B,C"}% will result in 'B' (not 'C'), %ENCODE{"asd" old="as,d" new="d,f"}% will yield 'df', and %ENCODE{"A" old="A,AA" new="AA,B"}% will give 'AA' and. %ENCODE{"asdf" old="a,asdf" new="a,2"}% will give 'asdf'
 Copyright © by the contributing authors. All material on this site is the property of the contributing authors.
Copyright © by the contributing authors. All material on this site is the property of the contributing authors.