newbie question here:

I need to create a function that will take in a string and replace any
ASCII characters greater than ASCII code 127 with their HTML equivalent.

E.g. something like: Replace(sInput, Chr(233), "é")
but to account for every other character than might get in there.

Would it be more cost-effective (processing-time wise) to just write
lots of replace functions one after the other, or should I loop through
the string and replace and characters as I encounter them.

How would I do this?

Thanks!
--

"I hear ma train a comin'
... hear freedom comin"

Re: replacing ASCII text by Steve

Steve
Thu Feb 17 06:51:06 CST 2005

Stimp wrote:

> I need to create a function that will take in a string and replace any
> ASCII characters greater than ASCII code 127 with their HTML equivalent.

Picking a small nit. Since ASCII is a 7-bit encoding scheme, there
are no ASCII characters with codes greater than 127. There is an
8-bit encoding scheme, variously called Extended ASCII or US-ASCII,
which has nothing to do with the American Standard Code for
Information Interchange.

> E.g. something like: Replace(sInput, Chr(233), "é")
> but to account for every other character than might get in there.

S = "String é with ê extended ë characters."

With New RegExp
.Pattern = "[\x80-\xFF]"
.Global = True
WScript.Echo .Replace(S, GetRef("MakeEntity"))
End With

Function MakeEntity(Match, Index, Source)
MakeEntity = "&#" & Asc(Match) & ";"
End Function

---
Replacement Functions in VBScript
http://msdn.microsoft.com/library/en-us/dnclinic/html/scripting121399.asp#scripting12_topic4

--
Steve

Martyrdom is the only way a person can become famous without ability.
-George Bernard Shaw

Re: replacing ASCII text by Stimp

Stimp
Thu Feb 17 07:16:52 CST 2005

On Thu, 17 Feb 2005 Steve Fulton <cerberus40@hotmail.com> wrote:
> Stimp wrote:
>
>> I need to create a function that will take in a string and replace any
>> ASCII characters greater than ASCII code 127 with their HTML equivalent.
>
> Picking a small nit. Since ASCII is a 7-bit encoding scheme, there
> are no ASCII characters with codes greater than 127. There is an
> 8-bit encoding scheme, variously called Extended ASCII or US-ASCII,
> which has nothing to do with the American Standard Code for
> Information Interchange.

ah well.. that's what I meant then :)

>> E.g. something like: Replace(sInput, Chr(233), "&#233;")
>> but to account for every other character than might get in there.
>
> S = "String é with ê extended ë characters."
>
> With New RegExp
> .Pattern = "[\x80-\xFF]"
> .Global = True
> WScript.Echo .Replace(S, GetRef("MakeEntity"))
> End With
>
> Function MakeEntity(Match, Index, Source)
> MakeEntity = "&#" & Asc(Match) & ";"
> End Function

very nice code there... I'm trying to get this working but having
trouble implementing it (I'm still quite new to VBscript)... here's my
code so far using what you gave me:

Function MakeEntity(Match, Index, Source)
MakeEntity = "&#" & Asc(Match) & ";"
End Function

Function ReplaceASCII(sInput)

With New RegExp
.Pattern = "[\x80-\xFF]"
.Global = True
.Replace sInput, GetRef("MakeEntity")
End With

ReplaceASCII = sInput
End Function

I'm not sure how to pass the output from the With statement to the
sInput string?

something like:

With...
sInput = .Replace sInput, GetRef("MakeEntity")
End With

?
Thanks
--

"I hear ma train a comin'
... hear freedom comin"

Re: replacing ASCII text by Steve

Steve
Thu Feb 17 17:09:24 CST 2005

Stimp wrote:

>>> I need to create a function that will take in a string and replace any
>>> ASCII characters greater than ASCII code 127 with their HTML equivalent.
>>
>>> E.g. something like: Replace(sInput, Chr(233), "&#233;")
>>> but to account for every other character than might get in there.
>
> here's my code so far using what you gave me:
>
> Function MakeEntity(Match, Index, Source)
> MakeEntity = "&#" & Asc(Match) & ";"
> End Function
>
> Function ReplaceASCII(sInput)
>
> With New RegExp
> .Pattern = "[\x80-\xFF]"
> .Global = True
> .Replace sInput, GetRef("MakeEntity")
> End With
>
> ReplaceASCII = sInput
> End Function

The Replace method returns a string with the replacement made; it doesn't
make changes to the source argument. You're throwing away the replaced
string.

> I'm not sure how to pass the output from the With statement to the
> sInput string?
>
> something like:
>
> With...
> sInput = .Replace sInput, GetRef("MakeEntity")
> End With

That will work (as long as you use parentheses around the arguments), but
it will change the original string (by default, function arguments are
ByRef). If you want to keep the original string and have your function
return the replacement string, you can either change your Function
definition:

Function ReplaceASCII(ByVal sInput)

or simply return the string returned from the Replace method.

With...
ReplaceASCII = .Replace(sInput, GetRef("MakeEntity"))
End With
End Function

If you want to modify the original string, you don't need a Function,
which returns a value; use a Sub.

Sub ReplaceASCII(ByRef sInput)
With...
sInput = .Replace(sInput, GetRef("MakeEntity"))
End With
End Sub

--
Steve

It takes two to speak the truth: one to speak, and another to hear.
-Henry David Thoreau