Jump to content

StringReplace special characters in htm file


david1337
 Share

Recommended Posts

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?

 

Link to comment
Share on other sites

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

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)

Link to comment
Share on other sites

27 minutes ago, jchd said:

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

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)

 

Link to comment
Share on other sites

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.

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)

Link to comment
Share on other sites

Hey guys, thanks for your answer.

2 hours ago, 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)

 

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

 

2 hours ago, 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.

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
Link to comment
Share on other sites

4 minutes ago, david1337 said:

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

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)

 

Link to comment
Share on other sites

56 minutes ago, 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)

 

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

Edited by david1337
Link to comment
Share on other sites

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
 Share

×
×
  • Create New...