Jump to content

Recommended Posts

Posted

Hey guys

Can anyone help me explain this? :)

$szFile = "test.htm"

$szText = FileRead($szFile)


$szText = StringReplace($szText, "hello", "ö")

FileDelete($szFile)
FileWrite($szFile,$szText)

If the file "test.htm" has it's text changed into something containing non US characters, in this example "ö", the output is " ö " when shown in a browser.
If i manually change the text in the "test.htm" file to "ö" - the output in the browser is "ö" !
In both cases, if the htm file is opened in notepad, the content is just "ö" - but the one changed from the script, still opens as " ö " in a browser. How weird is this?

I am aware that I can replace the text to " ö" , which is the HTML code for "ö" - then the output is correct in the browser, but this is just dumb when there are a lot of characters to be changed :)


Does anyone know why this happens, and how to solve it in a more simple way?

 

Posted

You're seeing UTF8 encoding of characters. Change the html header to indicate UTF8.

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted
  On 9/12/2016 at 9:04 AM, jchd said:

You're seeing UTF8 encoding of characters. Change the html header to indicate UTF8.

Expand  

Are you sure it's the header. I think it's the filewrite using the wrong mode.

@david1337: so test first:

$szFile = "test.htm"

$szText = FileRead($szFile)


$szText = StringReplace($szText, "hello", "ö")
$hFile=FileOpen($hFile,$FO_OVERWRITE + $FO_UTF8)
FileWrite($hFile,$szText)
FileClose($hFile)

 

Posted

Either you leave the header as ISO and use FileOpen with the ANSI mode to write the file,
or switch to full Unicode and switch the header to UTF8.

The later solution is universal, not the former.

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted (edited)

Hey guys, thanks for your answer.

  On 9/12/2016 at 9:38 AM, AutoBert said:

Are you sure it's the header. I think it's the filewrite using the wrong mode.

@david1337: so test first:

$szFile = "test.htm"

$szText = FileRead($szFile)


$szText = StringReplace($szText, "hello", "ö")
$hFile=FileOpen($hFile,$FO_OVERWRITE + $FO_UTF8)
FileWrite($hFile,$szText)
FileClose($hFile)

 

Expand  

You code gives an error: (Am I missing an include for or something for the code to understand what "$FO_UTF8" is?)
==> Variable used without being declared.:
$hFile=FileOpen($hFile,$FO_OVERWRITE + $FO_UTF8)
$hFile=FileOpen(^ ERROR

 

  On 9/12/2016 at 9:41 AM, jchd said:

Either you leave the header as ISO and use FileOpen with the ANSI mode to write the file,
or switch to full Unicode and switch the header to UTF8.

The later solution is universal, not the former.

Expand  

You are correct. Changing the HTML header to UTF-8 fixed the problem :)
But what if I do the same thing with a txt file, and open that in a web browser? Then I have the same problem, and I can't add a header to a txt file.
 

Edited by david1337
Posted
  On 9/12/2016 at 11:39 AM, david1337 said:

You code gives an error: (Am I missing an include for or something for the code to understand what "$FO_UTF8" is?)

Expand  

Yes a include is missing, but there's a 2. error (typo):

#include <FileConstants.au3>

$szFile = "test.htm"

$szText = FileRead($szFile)


$szText = StringReplace($szText, "hello", "ö")
$hFile=FileOpen($szFile,$FO_OVERWRITE + $FO_UTF8)
FileWrite($hFile,$szText)
FileClose($hFile)

and maybe you need other mode:

$hFile=FileOpen($szFile,$FO_OVERWRITE + $FO_ANSI)

 

Posted (edited)
  On 9/12/2016 at 11:49 AM, AutoBert said:

 

#include <FileConstants.au3>

$szFile = "test.htm"

$szText = FileRead($szFile)


$szText = StringReplace($szText, "hello", "ö")
$hFile=FileOpen($szFile,$FO_OVERWRITE + $FO_UTF8)
FileWrite($hFile,$szText)
FileClose($hFile)

 

Expand  

AutoBert, this was exactly what I was looking for, and it worked perfectly! Thanks a lot :)

Edited by david1337

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...