convertchars

[convertchars] along with [url] and [input] assure proper formatting and interpretation of text.

[convertchars] is a useful context for converting characters, and especially valuable because it enables the ability of defining custom conversions. It is used primarily for displaying database values on the browser page, and will make unsure upper ASCII characters display properly. Used without a specified database, it will convert a short list of common characters, such as curly quotes into straight quotes, and < and > into their html counterparts, this will prevent users from forcing html code into forms. When used without a nominated database, the conversions are sourced from the StandardConversions.db in the globals folder. This database may be customised to suit a project's needs.

[convertchars db=somedb.db][value][/convertchars]

Parameters

ParameterDescription
dbPath to conversion database which contains list of "from" and "to" conversions of words to other words. If not defined, the StandardConversions.db in the globals folder is used. In a custom database the two headers FROM and TO must be defined.
tableIn place of a db file, a WebDNA [table] may be used.
case(Optional) T or F to indicate that word comparisons should be case-sensitive or not. Default is F, case-insensitive
word(Optional) SS, WW, SW to indicate that words should be matched as SubString, Whole Word, or Start of Word, in the same way as when matching text in a database [search] is used.
delimitersNew from WebDNA 5.1
(Optional) List of single characters which define word boundaries.
[convertchars] secondary use is to clean data before inserting into a database, or 'cleaning' a file name when uploading and ensuring that it does not have any 'illegal' characters.

Example WebDNA code:
[convertchars]New Red Shoes[/convertchars]
[text]cleanfilename=[convertchars db=noillegals.db][filename][/convertchars][/text]

A conversion database can be called anything and located anywhere,
but must have these two specified fields: FROM and TO

Using this example WebDNA table named convert it is possible to convert the word "café" to "cafe"

[table name=convert&fields=from,to]
é    e
è    e
ê    e
ë    e
[/table]

Example WebDNA code:
[convertchars table=convert]café[/convertchars]
Results:

cafe

Some handy uses for [convertwords] include removing foul language from online message boards, spelling out acronyms, changing nicknames to full names, inserting hyperlinks, and expanding glossary terms.

Example WebDNA database:
-- conversion.db --
FROMTO
WebDNA<a href="www.webdna.us">WebDNA Software Corporation</a>
MikeMichael
foulwordf******d
support<a href="mailto:support@webdna.us">support</a>
logo<img src="companylogo.gif">

[convertchars] is excellent for replacing one character with one or multiple characters. Replacing a multiple character string with one or more other characters, then [convertwords] is the tool of choice.


[convertchars] does two relevant things differently from [convertwords]:

1) [convertchars] automatically UNURLs the TO and FROM fields in the table, so "%26" is treated as an "&" character rather than the literal "%26"
2) [convertchars] treats a "+" in the TO field as a space

There are reasons for these differences. The UNURLing of TO and FROM allows specification of invisible characters that you may need to strip out of or add to a data source. The + to SPACE mapping is a bit more esoteric, but I would bet the internal WebDNA program uses (or maybe "used") the same block of code for interpreting query parameters in a URL - the stuff after the ? - where spaces are mapped to + characters by the browser. For example "http://example.com/thispage.dna?user+name=Jane+Doe" gets a formvariable named "user name" with a value of "Jane Doe".

To illustrate convertchars vs. convertwords, I spent time working out these examples...

Example WebDNA code:
[table name=t1&fields=from,to]
& %26
[/table]
CHARS: [convertchars table=t1]L John & Joanna + Jane[/convertchars]
WORDS: [convertwords table=t1]L John & Joanna + Jane[/convertwords]

Example result:

CHARS: L John & Joanna + Jane
WORDS: L John %26 Joanna + Jane

Notes:
The & was replaced in both cases, but for convertchars it was replaced with & instead of literally %26



Example WebDNA code:

[table name=t1&fields=from,to]
%26 %2526
[/table]
CHARS: [convertchars table=t1]L John & Joanna + Jane[/convertchars]
WORDS: [convertwords table=t1]L John & Joanna + Jane[/convertwords]

Example result:
CHARS: L John %26 Joanna + Jane
WORDS: L John & Joanna + Jane


Notes:
convertchars treats the "to" value of "%26" as the character "&", and the "from" value of "%2526" as "%26" (after unurling %25 to %)
convertwords treated "%26" literally as "%26", so it did not find and replace the & character



Example WebDNA code:
[table name=t1&fields=from,to]
+ %252B
[/table]
CHARS: [convertchars table=t1]L John & Joanna + Jane[/convertchars]
WORDS: [convertwords table=t1]L John & Joanna + Jane[/convertwords]

Example result:
CHARS: L%2BJohn%2B&%2BJoanna%2B+%2BJane
WORDS: L John & Joanna %252B Jane


Notes:
convertchars treats the "to" value of "+" as the SPACE character, so all spaces were replaced with %2526, unurled to %2B, and the "+" character was not replaced
convertwords treated the to and from as entered, replacing "+" with, literally, "%2526"



Example WebDNA code:
[table name=t1&fields=from,to]
%2B %252B
[/table]
CHARS: [convertchars table=t1]L John & Joanna + Jane[/convertchars]
WORDS: [convertwords table=t1]L John & Joanna + Jane[/convertwords]



Example result:
CHARS: L John & Joanna %2B Jane
WORDS: L John & Joanna + Jane

Notes:
convertchars treats the "to" value of "%2B" as the "+" character, so "+" was replaced with %2526, unurled to %2B, and spaces were not replaced
convertwords found no literal "%2B" strings to replace



Example WebDNA code:
[table name=t1&fields=from,to]
' %27
+ %2B
# %23
% %25
& %26
! %21
" %22
[/table]
CHARS: [convertchars table=t1]L John & Joanna + Jane[/convertchars]
WORDS: [convertwords table=t1]L John & Joanna + Jane[/convertwords]

Example result:
CHARS: L+John+&+Joanna+++Jane
WORDS: L John %26 Joanna %2B Jane


Notes:
This is Michael's original example (with " + Jane" added to the source string).
convertchars treats the "to" value of "+" as the SPACE character, so:
all spaces were replaced with %2B, unurled to "+"
the "&" character was replaced with %26, unurled back to the "&" character
the "+" character was not replaced
convertwords treated the to and from as entered, so:
spaces were not changed
the "&" character was replaced with %26
the "+" character was replaced with %2B
So, in this case, the results of convertwords represent the desired outcome



Example WebDNA code:
[table name=t1&fields=from,to]
' %2527
%2B %252B
# %2523
% %2525
& %2526
! %2521
" %2522
[/table]
CHARS: [convertchars table=t1]L John & Joanna + Jane[/convertchars]
WORDS: [convertwords table=t1]L John & Joanna + Jane[/convertwords]


Example result:
CHARS: L John %26 Joanna %2B Jane
WORDS: L John %2526 Joanna + Jane


Notes:
From example 5, the "+" from value was replaced with "%2B" and the from "%" characters were replaced with "%25"
convertchars treats the "to" value of "%2B" as the "+" character, so:
all spaces were left alone
the "&" character was replaced with the desired "%26", after unurling "%25" to "%"
the "+" character was replaced with the desired "%2B"
convertwords treated the to and from as entered, so:
spaces were not changed
the "&" character was replaced with %2526 (not the desired %26)
the "+" character was not replaced
So, in this case, the results of convertchars represent the desired outcome



Example WebDNA code:
[grep search=[url]%20[/url]&replace= ][url]L John & Joanna + Jane[/url][/grep]

Example result:
L John %26 Joanna %2B Jane


Notes:
I'm not sure of the specifics of the desired mapping beyond what is shown, but if it is to URL everything except space characters, as it appears, then using the built in URL then reverting %20 to the space character does the trick in one line.