|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Internet and Network > HTP. - Command > !HTP-General - General HTTP operations and utilities > !Utility - Helper functions for HTTP operations (string conversion, proxy toggling) > HTP. - HTTP Operations |
SPR Script Language
HTP.FixEncoding
A utility command to repair common UTF-8 text display errors (Mojibake).
Intention
The HTP.FixEncoding command is a specialized tool for correcting garbled text that occurs when UTF-8 characters are misinterpreted.
For example, if a German word like "Grüße" is incorrectly displayed as "Grüße", this command can fix it by replacing the incorrect byte sequences with the proper Unicode characters.
It targets a list of known problematic patterns for German umlauts and common emojis. This command is useful as a last resort when an HTTP response provides text that is not strictly valid UTF-8, but whose errors are consistent and predictable.
Syntax
HTP.FixEncoding|$$INP[|$$OUT]
Parameter Explanation
P1 - $$INP - (Variable, String, Required)
The variable containing the garbled text that needs to be fixed.
P2 - $$OUT - (Variable, String, Optional)
The variable to store the corrected text. If omitted, the result is placed on the top-of-stack (TOS).
Examples
'***********************************
' HTP.FixEncoding - Sample 1: Correcting German Umlauts
'***********************************
VAR.$$BAD=This text is für an überprüfung.
HTP.FixEncoding|$$BAD|$$FIX
MBX.Corrected Text:|$$FIX
' $$FIX will now contain "This text is für an überprüfung."
END.
'***********************************
' HTP.FixEncoding - Sample 2: Fixing a raw response
'***********************************
VAR.$$URL=https://example.com/api/get_text
HTP.Request|GET|$$URL|||||$$RAW
' The raw response might be garbled. Fix it before displaying.
HTP.FixEncoding|$$RAW|$$RES
MBX.Server Response:|$$RES
END.
Remarks
- This is a "best-effort" utility and not a substitute for proper encoding handling. Its primary use is to clean up data from sources that do not correctly encode their UTF-8 output.
- The function targets a specific, hardcoded list of incorrect byte sequences for German characters and common emojis.
See also: