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