Jump to content

Why doesn't StringReplace() work?


cdeb
 Share

Recommended Posts

As for the object, in this case the StringReplace() function does not work for me.

These are my steps:

1) Extract the subject from an EML file.

ss_subject.png.3a6e11e1336dc70848f8ba459229b595.png (the first character is an emoji )

source from EML file: 

$sSubject = "?utf-8?B?8J+TiCA1IG9mIHRoZSBCZXN0IFN0b2NrcyB0byBCdXkgZm9yIERlY2VtYmVy?="

2) in this case I perform a decoding with _QuotedPrintable_DecodeEncodedWord()

output is: 

$sSubject = "?? 5 of the Best Stocks to Buy for December"

 

3) I perform StringReplace():

$sSubject = StringReplace($sSubject, "??", "")

or

$sSubject = StringReplace($sSubject, Chr(63)&Chr(63), "")

 

But the characters ?? they are not replaced.
 

Yet if I make an Asc of every character of the string with a for loop, are the first two characters really? that is 63

For cycle

        Local $aArray = StringSplit($sSubject, "", $STR_NOCOUNT)
        For $i = 0 To UBound($aArray)-1
            ConsoleWrite($aArray[$i] & " - " & Asc($aArray[$i])& @CR)
        Next

Output:        
? - 63
? - 63
  - 32
5 - 53
  - 32
o - 111
f - 102
  - 32
t - 116
h - 104
e - 101
  - 32

Does anyone have an explanation of why it doesn't work?
Thank you all

 

Edited by cdeb
Link to comment
Share on other sites

I can't use StringTrimLeft because it's not where and where the characters to be replaced will be.

Here is a snipped where StringReplace doesn't work.

there is also a for loop where the character is displayed? is the number 63

 

#include <Array.au3>


$sEML = "Message-ID: <20191203114629.18789103.20749@sailthru.com>"&@CRLF
$sEML &= "Subject: =?utf-8?B?8J+TiCA1IG9mIHRoZSBCZXN0IFN0b2NrcyB0byBCdXkgZm9yIERlY2VtYmVy?="&@CRLF
$sEML &= "MIME-Version: 1.0"&@CRLF

ConsoleWrite(">>> $sEML: "&@CRLF&$sEML&@CRLF)


$sSubject = _QuotedPrintable_DecodeEncodedWord($sEML)
ConsoleWrite(">>> $sSubject (_QuotedPrintable_DecodeEncodedWord): "&@CRLF&$sSubject&@CRLF)

$sSubject = StringReplace($sSubject, "??", "")  ; <---------------------------- ERROR?
ConsoleWrite(">>> $sSubject (StringReplace): "&@CRLF&$sSubject&@CRLF)



Local $aArray = StringSplit($sSubject, "", $STR_NOCOUNT)
For $i = 0 To UBound($aArray)-1
    ConsoleWrite($aArray[$i] & " - " & Asc($aArray[$i])& @CR)
Next



; ###############################################################################################################################
; ###############################################################################################################################
; ### Quoted-printable.au3 ######################################################################################################
; ###############################################################################################################################
; ###############################################################################################################################

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include-once
; #INDEX# =======================================================================================================================
; Title .........: UDF to Decode Word Encoded with Quoted Printable
; AutoIt Version : 3.3.10.2++
; Language ......: English
; Description ...:
; Author(s) .....: Prog@ndy
; Modified ......: mLipok
; ===============================================================================================================================
#cs
    Author original post
    https://autoit.de/index.php/Thread/11350-UTF8-Sting-Convertieren/?postID=87721#post87721

    Quoted-Printable
    http://tools.ietf.org/html/rfc2045#section-6.7
    https://www.ietf.org/rfc/rfc2045.txt
#CE

;~ _QuotedPrintable_Example()

Func _QuotedPrintable_Example()
    Local $sTestString = ''
    #forceref $sTestString
;~ https://autoit.de/index.php/Thread/11350-UTF8-Sting-Convertieren/?postID=87721#post87721
;~ $sTestString = "=?utf-8?b?QmFja3VwIEV4ZWMtTWVsZHVuZzogQXVmdHJhZyBlcmZvbGdyZWljaA==?="
;~ ;    $sTestString = "=?iso-8859-2?Q?3%_na_Otwartym_Koncie_Oszcz=EAdno=B6ciowym?="
;~ ;    $sTestString = "=?iso-8859-2?Q?EndFunc   ;=3D=3D>_MY_EXAMPLE_POP3?="
;~  MsgBox(0, '', _QuotedPrintable_DecodeEncodedWord($sTestString))
;~  MsgBox(0, '', _QuotedPrintable_DecodeEncodedWord("Subject: =?iso-8859-1?Q?=A1Hola,_se=F1or!?="))
EndFunc   ;==>_QuotedPrintable_Example

; #FUNCTION# ====================================================================================================================
; Name ..........: _QuotedPrintable_GetCodepage
; Description ...:
; Syntax ........: _QuotedPrintable_GetCodepage($charset)
; Parameters ....: $charset             - An unknown value.
; Return values .: None
; Author ........: Prog@ndy
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _QuotedPrintable_GetCodepage($charset)
    Local Const $PATH = "HKEY_CLASSES_ROOT\MIME\Database\Charset\"
    Local $alias
    While 1
        $alias = RegRead($PATH & $charset, "AliasForCharset")
        If @error Then ExitLoop
        $charset = $alias
    WEnd
    Local $result = RegRead($PATH & $charset, "InternetEncoding")
    Return SetError(@error, @extended, $result)
EndFunc   ;==>_QuotedPrintable_GetCodepage

; #FUNCTION# ====================================================================================================================
; Name ..........: _QuotedPrintable_MultiByteToWideChar
; Description ...:
; Syntax ........: _QuotedPrintable_MultiByteToWideChar($iCodePage, $dwFlags, $lpMultiByteStr, $cbMultiByte, $lpWideCharStr,
;                  $cchWideChar)
; Parameters ....: $iCodePage            - An unknown value.
;                  $dwFlags             - An unknown value.
;                  $lpMultiByteStr      - An unknown value.
;                  $cbMultiByte         - An unknown value.
;                  $lpWideCharStr       - An unknown value.
;                  $cchWideChar         - An unknown value.
; Return values .: None
; Author ........: Prog@ndy
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _QuotedPrintable_MultiByteToWideChar($iCodePage, $dwFlags, $lpMultiByteStr, $cbMultiByte, $lpWideCharStr, $cchWideChar)
    Local $TypeMBStr = "str"
    If IsPtr($lpMultiByteStr) Then $TypeMBStr = "ptr"
    Local $aResult = DllCall("Kernel32.dll", "int", "MultiByteToWideChar", "UINT", $iCodePage, "DWORD", $dwFlags, _
            $TypeMBStr, $lpMultiByteStr, "int", $cbMultiByte, "ptr", $lpWideCharStr, "int", $cchWideChar)
    If @error Then Return SetError(@error, 0, 0)
    Return $aResult[0]
EndFunc   ;==>_QuotedPrintable_MultiByteToWideChar

; #FUNCTION# ====================================================================================================================
; Name ..........: _QuotedPrintable_TranslateString
; Description ...:
; Syntax ........: _QuotedPrintable_TranslateString($sStringToTranslate, $iCodePage)
; Parameters ....: $sStringToTranslate              - An unknown value.
;                  $iCodePage            - An unknown value.
; Return values .: None
; Author ........: Prog@ndy
; Modified ......: mLipok
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _QuotedPrintable_TranslateString($sStringToTranslate, $iCodePage)
    SetError(0)
    If Not (IsInt($iCodePage) And $iCodePage > 0) Then $iCodePage = _QuotedPrintable_GetCodepage($iCodePage)
    If @error Or $iCodePage = 0 Then Return SetError(1, 0, "")

    Local $iLength = _QuotedPrintable_MultiByteToWideChar($iCodePage, 0, $sStringToTranslate, StringLen($sStringToTranslate), 0, 0)
    Local $Buffer = DllStructCreate("wchar[" & $iLength + 1 & "]")
    If Not _QuotedPrintable_MultiByteToWideChar($iCodePage, 0, $sStringToTranslate, StringLen($sStringToTranslate), DllStructGetPtr($Buffer), $iLength) Then
        Return SetError(2, 0, "")

    EndIf

    Return DllStructGetData($Buffer, 1)

EndFunc   ;==>_QuotedPrintable_TranslateString

; #FUNCTION# ====================================================================================================================
; Name ..........: _QuotedPrintable_Base64Decode
; Description ...:
; Syntax ........: _QuotedPrintable_Base64Decode($s)
; Parameters ....: $s                   - A string value.
; Return values .: None
; Author ........: Eddy
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _QuotedPrintable_Base64Decode($s)
    Local $key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', _
            $t = '', $p = -8, $a = 0, $c, $d, $len = StringLen($s)
    For $i = 1 To $len
        $c = StringInStr($key, StringMid($s, $i, 1), 1) - 1
        If $c < 0 Then ContinueLoop
        $a = BitOR(BitShift($a, -6), BitAND($c, 63))
        $p = $p + 6
        If $p >= 0 Then
            $d = BitAND(BitShift($a, $p), 255)
            If $c <> 64 Then $t = $t & Chr($d)
            $a = BitAND($a, 63)
            $p = $p - 8
        EndIf
    Next
    Return $t
EndFunc   ;==>_QuotedPrintable_Base64Decode

; #FUNCTION# ====================================================================================================================
; Name ..........: _QuotedPrintable_Decode
; Description ...:
; Syntax ........: _QuotedPrintable_Decode($s[, $IsQ = False])
; Parameters ....: $s                   - A string value.
;                  $IsQ                 - [optional] An unknown value. Default is False.
; Return values .: None
; Author ........: Prog@ndy
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _QuotedPrintable_Decode($s, $IsQ = False)
    If $IsQ Then $s = StringReplace($s, "_", " ")
    $s = StringSplit($s, "=")
    Local $result = $s[1]
    For $i = 2 To $s[0]
        $result &= Chr(Dec(StringLeft($s[$i], 2))) & StringTrimLeft($s[$i], 2)
    Next
    Return $result
EndFunc   ;==>_QuotedPrintable_Decode

; #FUNCTION# ====================================================================================================================
; Name ..........: _QuotedPrintable_DecodeEncodedWord
; Description ...:
; Syntax ........: _QuotedPrintable_DecodeEncodedWord($sStringToDecode)
; Parameters ....: $sStringToDecode           - A dll struct value.
; Return values .: None
; Author ........: Prog@ndy
; Modified ......: mLipok
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _QuotedPrintable_DecodeEncodedWord($sStringToDecode)
    Local $aEncodedChunks = StringRegExp($sStringToDecode, "=\?(.+?)\?(.)\?(.*?)\?=", 4)
    ;_ArrayDisplay($aEncodedChunks, "aEncodedChunks")
    If Not @error Then
        Local $aParts
        For $iChunk = 0 To UBound($aEncodedChunks) - 1
            $aParts = $aEncodedChunks[$iChunk]
            Switch $aParts[2]
                Case "B", "b"
                    $aParts[3] = _QuotedPrintable_Base64Decode($aParts[3])
                Case "Q", "q"
                    $aParts[3] = _QuotedPrintable_Decode($aParts[3], True)
            EndSwitch
            $aParts[3] = _QuotedPrintable_TranslateString($aParts[3], $aParts[1])
            $sStringToDecode = StringReplace($sStringToDecode, $aParts[0], $aParts[3])
        Next
    EndIf
    Return $sStringToDecode
EndFunc   ;==>_QuotedPrintable_DecodeEncodedWord

;~ Under construction
;~ Func _QuotedPrintable_DecodeEncodedText($sStringToDecode)
;~  StringSplit(
;~ EndFunc

 

Link to comment
Share on other sites

Looks like you're dealing with unicode characters. This revised code works for me --

#include <Array.au3>


Global $sEML = "Message-ID: <20191203114629.18789103.20749@sailthru.com>"&@CRLF
$sEML &= "Subject: =?utf-8?B?8J+TiCA1IG9mIHRoZSBCZXN0IFN0b2NrcyB0byBCdXkgZm9yIERlY2VtYmVy?="&@CRLF
$sEML &= "MIME-Version: 1.0"&@CRLF

ConsoleWrite(">>> $sEML: "&@CRLF&$sEML&@CRLF)


Global $sSubject = _QuotedPrintable_DecodeEncodedWord($sEML)
ConsoleWrite(">>> $sSubject (_QuotedPrintable_DecodeEncodedWord): "&@CRLF&$sSubject&@CRLF)

$sSubject = StringReplace($sSubject, ChrW(55357) & ChrW(56520), "")  ; <---------------------------- ERROR?
ConsoleWrite(">>> $sSubject (StringReplace): "&@CRLF&$sSubject&@CRLF)



Global $aArray = StringSplit($sSubject, "", $STR_NOCOUNT)
For $i = 0 To UBound($aArray)-1
    ConsoleWrite($aArray[$i] & " - " & AscW($aArray[$i])& @CR)
Next

 

Link to comment
Share on other sites

18 minutes ago, Danp2 said:

This revised code works for me

For me as well.

To other interested users:
It is not necessary to integrate the source code of the "quoted-printable.au3" into your script. You can download the POP3.au3 UDF 2.0.1  by @mLipok . The Zip-file contains this .au3 - you can #include it , as usual.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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...