Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP converts the htmlentity? 1

Status
Not open for further replies.

Olavxxx

Programmer
Sep 21, 2004
1,134
NO
Hi,

I have/had problems with Norwegian characters like æ å and the ø (theese are the html-entities).

I know there is a function in ASP, the Server.HTMLEncode(), which can convert theese and more.

The problem is that I also have HTML inside the string, as this is my custom CMS.

This means I cant use the Server.HTMLEncode(), as then it would render the HTML like <img.. /> as pure text!

So, I made my own function, with a dictionary.

Its quite simple and it worked like a charm!

The output of my dummy-function was:
Ibsens ripsb&aelig;rbusker og andre buskevekster<hr />
Ibsens ripsbærbusker og andre buskevekster

And this is correct! It replaces the keys with the items..

Here is my code:
Code:
Function replaceChars(strInput)
	set d=Server.CreateObject("Scripting.Dictionary")
	d.Add "Æ", "&AElig;"
	d.Add "æ", "&aelig;"
	d.Add "ø", "&oslash;"
	d.Add "Ø", "&Oslash;"
	d.Add "Å", "&Aring;"
	d.Add "å", "&aring;"
	'd.Key("i") = "it"
	'Response.Write("The key i has been set to it, and the value is: " & d.Item("n") & " " & d.Count)
	
	Dim counter, keys, items
	keys = d.Keys
	items = d.Items
	
	counter=0
	for counter = 0 to UBound(keys)
		strInput = Replace(strInput, keys(counter), items(counter))
	next 
	
	response.write (strInput & "<hr />" & text)
End Function

Yes, I told you the function works.. And so it does..
However, my problem here.. and what scratches my head, is that when I post to the ASP file and send the posted value to the function, it for some reason does not work :S

This is how I do it, when it doesent work:
Or, it does work.. I know it replaces, as I tried adding more keys and items to the dictionary and it actually replaces the items, but for some reason it seems ASP then decodes it :S

I slightly modified the function to work with my posted asp page:
Code:
Function replaceChars(strInput)
	Dim strOutput
	strOutput = strInput
	
	set d=Server.CreateObject("Scripting.Dictionary")
	d.Add "Æ", "&AElig;"
	d.Add "æ", "&aelig;"
	d.Add "ø", "&oslash;"
	d.Add "Ø", "&Oslash;"
	d.Add "Å", "&Aring;"
	d.Add "å", "&aring;"
	
	Dim counter, keys, items
	keys = d.Keys
	items = d.Items
	
	counter=0
	for counter = 0 to UBound(keys)
		strOutput = Replace(strOutput, keys(counter), items(counter))

	next 
		'Response.Write (strOutput & " - " & strInput & "<hr />")	
	fixText = strInput
End Function

Then I do it like this:
Code:
'Add a new record to the recordset
replaceChars(Request.Form("txtSubject"))
rsAddComments.Fields("innhold_tittel") = fixText

replaceChars(Request.Form("txtIngress"))
rsAddComments.Fields("innhold_ingress") = fixText

replaceChars(Request.Form("txtTekst"))
rsAddComments.Fields("innhold_tekst") = fixText

I hope someone can shed some light on this "issue".
AFAIK, the function works, and it replaces the occurrances of the keys, with the items..

Eg. to test it, I fed it with my name: "Olav Alexander Mjelde" and I added another dictionary item, which then replaced my last name with "test". This worked perfectly.. But it SEEMS it does not replace the occurrances of the Norwegian characters (but I belive it does!). I believe ASP for some reason decodes the characters back to theire non-entity form(?). I hope this comes out clear..

Olav Alexander Mjelde
Admin & Webmaster
 
btw. I see an error in the code now..
fixText = strInput, should be fixText = strOutput.

However, this is not the issue.. This error I made just before posting the code here.. I tried making the strOutput to compare it with strInput. Before I had only strInput..

eg. So I know that error :p but that's not the issue!

Olav Alexander Mjelde
Admin & Webmaster
 
Suppose you do these.
[1] In the served page, specified the character set to be utf-8 by adding a meta tag in the head.
[tt] <meta http-equiv="content-type" content="text/html; charset="utf-8">[/tt]
[2] In the page posted to, at the topmost of the (asp) page, specify again the code page.
[tt] <%
Session.CodePage=65001
Response.Charset="utf-8"
'etc
%>[/tt]
[3] When you response.write again, continue pass the string through server.HTMLEncode() or your custom function.
[4] When you process the posted data, just get them out of request.form("xyz") as usual.
 

er... isn't &AElig; a HTML entity ? In which case, why are you trying to convert it ? Send the entity name to the browser... let the browser do it's job.

HTMLEncode() converts strings of actual text (e.g. international chars etc) into HTML safe entities..

try this in a web page:

Code:
&AElig;

just that, nothing else, should produce the char you want... that's why they invented these things.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Damber: Yes, I am trying to convert it to htmlentities and it works in my function, as long as I feed the string from the same page.

However, when I use the posted data from the previous page, it seems it converts it from htmlentities back to the regular char...

I cant use the htmlencode(), as that would encode also the <img..> etc. (its my custom content management system).

Tsuji: The posting page already had utf-8.
However, when I added your code
Code:
    Session.CodePage=65001
    Response.Charset="utf-8"
to the .asp page, it meerly strips away all the Norwegian characters.. So if I send the string "æøå", it equals "" when it reaches the .asp page..

very strange indeed :p

Olav Alexander Mjelde
Admin & Webmaster
 

you've got a lot of mess in your code.

1. Why do you create a dictionary object, then copy the values out to two arrays ??? why not just create two arrays, or just one multidimensional one, or just one using offsets ?
2. fixText - where is this defined..? Why not just return as a value of the function ??
3. why do you create strOutput, replace characters in it, then return strInput ?
4. you seem to be adding to a database field after 'fixing'... what do you do to present that data to the browser ?

maybe 3 is the source of your issue... or maybe 4 - in which case show the code that actually displays the data...



A smile is worth a thousand kind words. So smile, it's easy! :)
 
The 3) was a typo before I posted the code here..
As far as the fixing variable, its set in the page top.. but each time the function is called, its set to the new value.

This is not the issue though! The correct data gets passed to the database.. I can read it in the db!

It also replaces.. eg. if I add NO => Norway in the dictionary and then write the text: I am from NO, where are you from?

It will say in the db:
I am from Norway, where are you from?

So this all works.. The only issue is the fact that it for some reason doesent use the htmlentities as it did before I integrated it with the post.

Olav Alexander Mjelde
Admin & Webmaster
 

er... maybe you want to re-read my last post, especially the last part of the last sentence.



A smile is worth a thousand kind words. So smile, it's easy! :)
 
>So if I send the string "æøå", it equals "" when it reaches the .asp page..
I think the issue is worth testing throughly. I suspect the setup is not complete. Let's take a little application and see what's is going on.

[1] Take a simple login type page.
[tt]
<!-- login.htm -->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset="utf-8">
</head>
<body>
<form name="formname" method="post" action="login_resp.asp">
<input name="usr" type="text" /><br />
<input name="pwd" type="password" /><br />
<input name="submitbtn" type="submit" /><br />
</form>
</body>
</html>
[/tt]
[2] Make up a login_resp.asp on the server of the same directory.
[tt]
<%
Session.CodePage=65001
Response.Charset="utf-8"

susr=request.form("usr")
spwd=request.form("pwd")
response.write server.HTMLEncode(susr) & "<br />" & server.HTMLEncode(spwd) & "<br />" & vbcrlf
%>
[/tt]
[3] When load up the login.htm, you sure can enter some arbitrary username and password on the Norvegian keyword. In particular, enter some Scandinavian alphabet such as æ,ø,å (English keyboard "'",";","[" - modulo all the variations of the keyboard!!!) and submit the page. My post above purport you get the request properly read on the server and can readily use to query dbase backend. Whereas, while displaying on the client m/c as a feedback, the characters are properly display as well.

[4] If you can reproduce the correct behaviour, you can comment this or that to see the effect. I think what I suggest is to the precision that not one functional line is vain. You can inflat the pages with irrelevant, sure.

[5] Furthermore, after setting it up, you can as well change the encoding to something other than utf-8 like iso-8859-1 readily operational for Scandinavian alphabets.
 
Hi, the same thing still happend!

I made the HTML file and I made the ASP file you recommended.

Then I wrote:
abcæøå

On the asp page, it shows:
abc

I also tried the
Code:
Response.Charset="iso-8859-1"

In the .asp page, but the results where the same!
(Norwegian characters where stripped).

When I removed the entire response.charset and tried again, my results where better.

It now got "abc &#230;&#248;&#168;" as the result, when I removed the Response.Charset entirly.

---

Ok.. So, then I put my function there.. And it worked!

eg. This worked perfectly:
Code:
<%
'Session.CodePage=65001
'Response.Charset="iso-8859-1"

susr=request.form("usr")
spwd=request.form("pwd")
'response.write server.HTMLEncode(susr) & "<br />" & server.HTMLEncode(spwd) & "<br />" & vbcrlf

replaceChars(susr)
replaceChars(spwd)

Function replaceChars(strInput)

    set d=Server.CreateObject("Scripting.Dictionary")
    d.Add "Æ", "&AElig;"
    d.Add "æ", "&aelig;"
    d.Add "ø", "&oslash;"
    d.Add "Ø", "&Oslash;"
    d.Add "Å", "&Aring;"
    d.Add "å", "&aring;"

    Dim counter, keys, items
    keys = d.Keys
    items = d.Items

    counter=0
    for counter = 0 to UBound(keys)
        strInput = Replace(strInput, keys(counter), items(counter))

    next
    Response.Write (strInput)
End Function

%>

I should be so happy, happy, happy, happy..
ONLY it doesent work when I submit from the page I have :S

So, I wonder if it all may bottom down to some kindof session set by the system I use the template and send from.

Even though the charset is set to the same, it simply does not work, if I submit from the .fwx page (fotoweb).

I use the IDENTICAL function which worked from index.html to the .asp page, but when I view source in the asp page, it shows no htmlentities...

so.. yeah.. I guess I might have to make the input page without using the template-engine from fotoweb, as a fotoweb setting might be what's causing this all! I'll make an asp page submitting from instead, but this means its more work to control logged in/access, etc.

Ill try a bit more, but I'm pretty sure it bottles down to the issue. I however know this fotoweb test-site is set up with wrong language, as the <translate></translate> doesent translate, as our live site does.

eg. I should maybe try my code on the "Live site", before I bury it all :p As it might work there.. hmm..


Anyways, thanks for your suggestions, but it all bottles down to the .fwx system, AFAIK.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top