This tag loops through an array of pairs using the values in the pairs to find substrings and replace them with other strings. It does not modify the string in place, but returns a new string I used an array of pairs instead of a map so that the order could be specified. This allows earlier replacements to contain text that later gets replaced Required Paramaters: -------------------- a_string should contain the string to process replacements should contain an array of pairs with - the first item in each pair should be the string to find - the second item in each pair should be the replacement string
Parameters
-a_string
string, required
The string to search and replace on
-replacements
array, required
An array of pairs whose values correspond to find=replace
Sample Usage
lsd_string_replacemultiple('Now is the REPLACE_ME to come to the aid AND_ME_TOO',array(pair('REPLACE_ME'='for all good men'),pair('AND_ME_TOO'='of their country')));
//This also works:
lsd_string_replacemultiple('Now is the REPLACE_ME to come to the aid AND_ME_TOO',array('REPLACE_ME'='for all good men', 'AND_ME_TOO'='of their country'));
Source Code
Click the "Download" button below to retrieve a copy of this tag,
including the complete documentation and sample usage shown
on this page. Place the downloaded ".inc" file in your
LassoStartup folder, restart Lasso, and you can begin using this
tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
define_tag('LSD_String_ReplaceMultiple', -required='a_string', -required='replacements', -priority='replace');
// Processes a string replacing based on an array of pairs and returns the new string
// (Note, I used an array of pairs instead of a map so that the order could be specified)
// a_string should contain the string to process
// replacements should contain an array of pairs with
// - the first item in each pair should be the string to find
// - the second item in each pair should be the replacement string
local('result') = #a_string;
iterate(#replacements, local('replace_set'));
#result->replace(#replace_set->first, #replace_set->second);
/iterate;
return(#result);
/define_tag;