TextEncode (English)
For the original Italian page, please click on the link: http://www.guru4.net/articoli/text-encode/default.aspx
Often it is found to having to us to insert in the Web pages of the coming from text from text documents (rows of Word, email, etc.) and us it must be met with the insertion of "particular characters": accented letters, blocks of code or... Japanese characters who are! In order to visualize our page HTML correctly we can:
to use a good ones editor, that it automatically carries out the escape of the characters (not, the notepad unfortunately does not make it)
to set up in the page the correct table as an example codes (defining charset=iso-8859-1 for the Latin characters, but often is not sufficient in order to manage all the characters demands from one page)
to have good memory (and much patience) and to manually replace the characters who do not come it correctly visualizes to you (is not my case: in more I succeed to remember 3 or 4 entities... "">" for ">", "<" for "<" and "&" for "&"...)
to use an application that makes the job to the place ours, that is: the objective of this article!
The layout
Our application will not be other that a simple
page ASP.NET that will introduce one case of text (textarea) in which
incollera the text "to encodare" and a push-button in order to recall
the code and to obtain the text to insert in page HTML. We will
then add some options for being able to control the result ulteriorly
of codifies as an example (in order to define like dealing the returns
to head or the characters of spaziatura and tabulazione).
After all the controls to insert in the page are:
A Textarea in order to insert the text originates them (SourceText)
A Textarea that will contain the result of the operation of encode (EncodedText)
One DropDownList in order to define the modality of management of the returns to head (EndLineReplaceMode)
One DropDownList in order to define the modality of management of the characters of tabulazione (TabReplaceSpaceNumber)
One CheckBox in order to define if to use the entity " " in the substitution of the characters of tabulazione (TabEscapeSpaces)
One CheckBox in order to define if to replace the characters of spaziatura with the entity " " (EscapeSpaces)
A Button for the shipment of form and the execution of escaping (the StartEncode)
The aforesaid controls, for comfort, will have the attribute runat="server".
The code of page ASP.NET
Since, given the objective of the same page, the shipment of code HTML will be admitted, adds to the page directives the disabilitazione of the validazione of the Request:
<%@ Page Language="C#" ValidateRequest="false"%>
We insert in body (BODY) of the page form with the controls the demands:
<form id="frmTextEncode" runat="server" method="post">
<textarea id="SourceText" runat="server" cols="50" rows="10"></textarea>
<textarea id="EncodedText" runat="server" cols="50" rows="10"></textarea>
<label for="EndLineReplaceMode">Fine riga e ritorno a capo:</label>
<select id="EndLineReplaceMode" runat="server">
<option value="text">Non sostituire</option>
<option value="html">Sostituisci con <br></option>
<option value="xml">Sostituisci con <br /></option>
</select>
<br /><br />
<label for="TabReplaceSpaceNumber">Caratteri di tabulazione:</label>
<select id="TabReplaceSpaceNumber" runat="server">
<option value="0">Non sostituire</option>
<option value="1">Sostituisci con 1 spazio</option>
<option value="2">Sostituisci con 2 spazi</option>
<option value="3">Sostituisci con 3 spazi</option>
<option value="4">Sostituisci con 4 spazi</option>
<option value="5">Sostituisci con 5 spazi</option>
<option value="6">Sostituisci con 6 spazi</option>
<option value="7">Sostituisci con 7 spazi</option>
<option value="8">Sostituisci con 8 spazi</option>
</select>
<input type="checkbox" id="TabEscapeSpaces" value="1" runat="server" checked="true" /><label for="TabEscapeSpace">utilizza " " come carattere di spaziatura</label>
<br /><br />
<input type="checkbox" id="EscapeSpaces" value="1" runat="server" /><label for="EscapeSpace">Sostituisci i caratteri di spaziatura con " "</label>
<br /><br />
<asp:Button ID="StartEncode" runat="server" OnClick="StartEncode_Click" Text="Invia" />
</form>
We add a block of serveur-side script in which inserting the code to execute to the click of the push-button:
<script runat="server">
protected void StartEncode_Click(object sender, EventArgs e)
{
}
</script>
Come.primo.cosa we recover the text for which the encoding is demanded:
string t = this.SourceText.Value;
The management of the characters for which it turns out
necessary the encode is entrusted to the method HtmlEncode of the class System.Web.HttpUtility of NET the Framework; insomma: more it is
made almost... gratis!
t = System.Web.HttpUtility.HtmlEncode(t);
To we it does not remain that to manage the remaining
substitutions, relative to the returns to head, the characters of
tabulazione and those of spaziatura.
We begin verifying the demand for substitution of the spaces
with relative entity HTML:
if(this.EscapeSpaces.Checked)
t = t.Replace(" ", " ");
Therefore we verify the modality selected for the management of the returns to head (HTML or XML) and, eventually, we proceed to the substitution:
switch (this.EndLineReplaceMode.Value)
{
case "text":
// no replace
break;
case "html":
t = t.Replace("\r\n", "<br>\r\n");
break;
case "xml":
t = t.Replace("\r\n", "<br />\r\n");
break;
}
In the same way we replace the characters of tabulazione with the selected format (n you space or n entity ):
string s = "";
string sc = (this.TabEscapeSpaces.Checked) ? " " : "";
int sn = System.Convert.ToInt32(this.TabReplaceSpaceNumber.Value);
for(int i = 0; i < sn; i )
s = sc;
if(sn > 0)
t = t.Replace("\t", s);
In order to end we set up the text in the Textarea of output:
this.EncodedText.Value = t;
Conslusioni
For having always to capacity of... mouse this comfortable one utility you can add to yours bookmarks the demonstrative page available on-linens.