parseText: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - " |r1=[[" to " |r1= [[")
m (Optimized escaping function using regex, up to 10x faster)
Line 53: Line 53:
<dt class="note">[[User:AgentRevolution|AgentRev]]</dt>
<dt class="note">[[User:AgentRevolution|AgentRev]]</dt>
<dd class="note">
<dd class="note">
If you are parsing user-defined strings like player names, you may want to convert special characters to their {{Wikipedia|Character_encodings_in_HTML#XML_character_references|XML entities}} as a precaution beforehand:
If you are parsing user-defined strings like player names, you may want to convert special characters to their {{Wikipedia|Character_encodings_in_HTML#XML_character_references|XML entities}} as a precaution beforehand, otherwise the result might be corrupted.
<code>private _encodeText =
<code>private _escapeXML =
{
{
private _specialChars = [38, 60, 62, 34, 39]; //  &amp; < > " '
_this
private _convertTo = [[38,97,109,112,59], [38,108,116,59], [38,103,116,59], [38,113,117,111,116,59], [38,97,112,111,115,59]]; //  &amp;amp; &amp;lt; &amp;gt; &amp;quot; &amp;apos;
[[regexReplace]] ["&amp;", "&amp;amp;"]
private _chars = [];
regexReplace ["<", "&amp;lt;"]
private "_i";
regexReplace [">", "&amp;gt;"]
 
regexReplace ['"', "&amp;quot;"]
{
regexReplace ["'", "&amp;apos;"]
_i = _specialChars find _x;
if (_i isEqualTo -1) then { _chars pushBack _x } else { _chars append (_convertTo select _i) };
} forEach toArray param [0,"",[""]];
 
toString _chars
};
};


private _encodedName = _name call _encodeText;
private _name = '<A&amp;M> Paul "Bear" Bryant';
private _redName = parseText format ["<t color='#ff0000'>%1</t>", _encodedName];</code>
private _escapedName = _name call _escapeXML;
private _redName = [[parseText]] format ["<t color='#ff0000'>%1</t>", _escapedName];</code>
</dd>
</dd>


</dl>
</dl>

Revision as of 20:05, 5 October 2021

Hover & click on the images for description

Description

Description:
Creates a structured text by parsing the given XML description. Do not use parseText when displaying a localized text from Stringtable.xml.
A detailed explanation of all formatting possibilities can be found at Structured Text.
Groups:
StringsStructured Text

Syntax

Syntax:
parseText text
Parameters:
text: String
Return Value:
Structured Text

Examples

Example 1:
private _Stxt = parseText "First line<img image='data\isniper.paa'/><br/>Second line";
Example 2:
hintSilent parseText format["<t size='1.25' font='Zeppelin33' color='#ff0000'>%1 lives remaining.</t>", 12];
Example 3:
private _clickableLink = parseText "<a href='http://arma3.com'>A3</a>";

Additional Information

See also:
Structured TextcomposeTexttextlocalizectrlSetStructuredText

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note


Posted on 1 September 2016
Dr_Eyeball
To convert a structured text back to a string type, just use: _str = str parseText _structuredText;
Posted on September 1, 2017 - 13:23 (UTC)
AgentRev
If you are parsing user-defined strings like player names, you may want to convert special characters to their XML entities as a precaution beforehand, otherwise the result might be corrupted. private _escapeXML = { _this regexReplace ["&", "&amp;"] regexReplace ["<", "&lt;"] regexReplace [">", "&gt;"] regexReplace ['"', "&quot;"] regexReplace ["'", "&apos;"] }; private _name = '<A&M> Paul "Bear" Bryant'; private _escapedName = _name call _escapeXML; private _redName = parseText format ["<t color='#ff0000'>%1</t>", _escapedName];