glosub()

 
Global substitution returns a copy of string in which all instances of string search have been replaced by string replace. (Note: to represent a backslash () in replace, use '\\').
Syntax
glosub( target_string, search, replace )
Returns string where all instances of search have been replaced.
  • target_string = the target string
  • search = the sub-string that will be search ed for
  • replace = the replacement string
User Annotations: glosub
Ray Yates : rayyates1 at, gmail d0t com
12/28/2018 16:54 p.m.
Replaces a single case sensitive occurrence of a sub-string within a string.

string = The target string getting searched.
search = The string to search for
replace = The string to replace
start = the position to begin searching. After the replace, start will contain the next character in the string after the replace.
<MvFUNCTION NAME = "Replace" PARAMETERS = "string, search, replace, start VAR" STANDARDOUTPUTLEVEL = "">
    <MvCOMMENT>
        Replace: Replaces a single case sensitive occurrence of a sub-string within a string.
            Accepts a starting search position (usually 1). Start is changed to the first char position AFTER the replaced string.

            <MvASSIGN NAME="g.string" VALUE="{ 'Now is the time for all good men to come to the aid of their nation.' }">
            <MvEVAL EXPR="{ Replace(g.string, 'nation', 'country', g.start) }"><br>
            <MvEVAL EXPR="{ g.start }">

    </MvCOMMENT>
    <MvASSIGN NAME = "l.savestring" VALUE = "{ l.string }">
    <MvIF EXPR = "{ l.start LT 1 }">
        <MvASSIGN NAME = "l.start" VALUE = "{ 1 }">
    </MvIF>

    <MvIF EXPR = "{ l.start GT 1 }">
        <MvASSIGN NAME = "l.left" VALUE = "{ substring(l.string,1,l.start -1) }">
    </MvIF>
    <MvASSIGN NAME = "l.string" VALUE = "{ substring(l.string,l.start,len(l.string)) }">

    <MvASSIGN NAME = "l.posn" VALUE = "{ l.search IN l.string }">
    <MvIF EXPR = "{ l.posn GT 1 }">
        <MvASSIGN NAME = "l.left" VALUE = "{ l.left $ substring(l.string,1,(l.posn -1)) }">
        <MvASSIGN NAME = "l.string" VALUE = "{ substring(l.string,l.posn,len(l.string)) }">
    </MvIF>

     <MvIF EXPR = "{ (l.search IN l.string ) EQ 1 }">
        <MvASSIGN NAME = "l.right" VALUE = "{ substring(l.string,len(l.search)+1,len(l.string)) }">
        <MvASSIGN NAME = "l.start" VALUE = "{ len(l.left $ l.replace) + 1 }">
        <MvASSIGN NAME = "l.return" VALUE = "{  l.left $ l.replace $ l.right }">
        <MvIF EXPR = "{ l.start GT len(l.return) }">
            <MvASSIGN NAME = "l.start" VALUE = "{ 0 }">
        </MvIF>
    <MvELSE>
        <MvASSIGN NAME = "l.return" VALUE = "{ l.savestring }">
        <MvASSIGN NAME = "l.start" VALUE = "{ 0 }">
    </MvIF>

    <MvFUNCRETURN VALUE = "{ l.return }">
</MvFUNCTION>
Ray Yates : mivascript at, pcinet d0t com
08/21/2014 12:42 a.m.
Returns the source string after removing each character in chars.
<MvFUNCTION NAME = "RemoveChars" PARAMETERS = "source, chars" STANDARDOUTPUTLEVEL = "">
    <MvCOMMENT> Usage g.phone = RemoveChars(g.phone,'()-,.') </MvCOMMENT>
    <MvIF EXPR = "{ l.chars }">
        <MvASSIGN NAME = "l.posn" VALUE = "{ 1 }">
        <MvASSIGN NAME = "l.char" VALUE = "{ substring(l.chars,l.posn,1) }">
        <MvWHILE EXPR = "{ l.char }">
            <MvASSIGN NAME = "l.source" VALUE = "{ glosub(l.source, l.char, '') }">
            <MvASSIGN NAME = "l.posn" VALUE = "{ l.posn + 1 }">
            <MvASSIGN NAME = "l.char" VALUE = "{ substring(l.chars,l.posn,1) }">
        </MvWHILE>
    </MvIF>
    <MvFUNCRETURN VALUE = "{ l.source }">
</MvFUNCTION>
Ray Yates : mivascript at, pcinet d0t com
08/02/2011 22:52 p.m.
Searches for each word in [banned] a comma seperated list of words, in a text string. If any word is found it is returned.

It uses the previously posted function ReplaceChars(). These two function form the basis for the spam filter used by the Annotation program on this site.
<MvFUNCTION NAME = "IsBanned" PARAMETERS = "banned var, string var" STANDARDOUTPUTLEVEL = "">
    <MvASSIGN NAME = "l.words" VALUE = "{ miva_array_deserialize(l.banned) }">
    <MvASSIGN NAME = "l.maxwords" VALUE = "{ miva_array_max(l.words) }">

    <MvASSIGN NAME = "l.textstring" VALUE = "{ ReplaceChars(l.string,'~`!@#$%^&*()_-+={}|[]:;<>?,./' $ asciichar(34) $ asciichar(39), ' ') }">
    <MvASSIGN NAME = "l.textstring" VALUE = "{ glosub(glosub(glosub(l.textstring,'    ',' '),'   ',' '),'  ',' ') }">

    <MvASSIGN NAME = "l.posn" VALUE = "{ 1 }">
    <MvWHILE EXPR = "{ l.posn LE l.maxwords }">

        <MvIF EXPR = "{ l.words[l.posn] }">
            <MvIF EXPR = "{ l.words[l.posn] CIN l.textstring}">
                <MvASSIGN NAME = "l.found" VALUE = "{ l.words[l.posn] }">
                <MvWHILESTOP>
            </MvIF>
        </MvIF>
        <MvASSIGN NAME = "l.posn" VALUE = "{ l.posn + 1 }">
    </MvWHILE>

    <MvFUNCRETURN VALUE = "{ l.found }">
</MvFUNCTION>
Ray Yates : mivascript at, pcinet d0t com
08/02/2011 22:43 p.m.
Search the [source] string for each character in the string [chars]. If found replace it with the string or charcters in [with].

The example removes all punction characters from a string to make it more searchable.
<MvASSIGN NAME = "l.text_only" VALUE = "{ ReplaceChars(l.string,'~`!@#$%^&*()_-+={}|[]:;<>?,./' $ asciichar(34) $ asciichar(39), ' ') }">

<MvFUNCTION NAME = "ReplaceChars" PARAMETERS = "source, chars, with" STANDARDOUTPUTLEVEL = "">
    <MvCOMMENT> Usage g.no_characters = RemoveChars(g.string,'()-,.', ' ') </MvCOMMENT>
    <MvIF EXPR = "{ l.chars }">
        <MvASSIGN NAME = "l.posn" VALUE = "{ 1 }">
        <MvASSIGN NAME = "l.char" VALUE = "{ substring(l.chars,l.posn,1) }">
        <MvWHILE EXPR = "{ l.char }">
            <MvASSIGN NAME = "l.source" VALUE = "{ glosub(l.source, l.char, l.with) }">
            <MvASSIGN NAME = "l.posn" VALUE = "{ l.posn + 1 }">
            <MvASSIGN NAME = "l.char" VALUE = "{ substring(l.chars,l.posn,1) }">
        </MvWHILE>
    </MvIF>
    <MvFUNCRETURN VALUE = "{ l.source }">
</MvFUNCTION>