strreplace—replace parts of a string


The strreplace function replaces parts of a string. The syntax for this function is as follows:

strreplace($ORGSTR,$SEARCH,$REPLACE,$NEWSTR)
$NEWSTR=strreplace($ORGSTR,$SEARCH,$REPLACE)

strreplace($ORGSTR,$SEARCH,$REPLACE,$COUNT,$NEWSTR)
$NEWSTR=strreplace($ORGSTR,$SEARCH,$REPLACE,$COUNT)

strreplace($ORGSTR,$SEARCH,$REPLACE,$COUNT,$SKIP,$NEWSTR)
$NEWSTR=strreplace($ORGSTR,$SEARCH,$REPLACE,$COUNT,$SKIP)

strreplace arguments

Argument

Mode

Type

Description

$ORGSTR

Input

STRING

The original string from which parts are to be replaced

$SEARCH

Input

STRING

Specifies the part of the original string that you want to search for and replace

$REPLACE

Input

STRING

Specifies the string that you want to replace the search string with

$COUNT

Input

INTEGER

Specifies the maximum number of replacements

$SKIP

Input

INTEGER

Specifies the number of $SEARCH strings to skip before replacement begins

$NEWSTR

Output

STRING

The new string that is created as a result of the replacement

Use the strreplace function to produce $NEWSTR from the original string ($ORGSTR) by replacing all occurrences of $SEARCH with $REPLACE. Replacement is performed from beginning of the string to the end and is not recursive.

If desired, you can limit the number of replacements to $COUNT. Replacements start at the beginning of the string until $COUNT number of replacements have occurred. 

You can also specify a number of occurrences to skip ($SKIP) from the beginning of the string before starting replacements.

strreplace examples

The following example replaces all spaces with an underscore.

$EV.msg = strreplace($EV.msg,' ','_')

All spaces are replaced with underscores for the msg slot of the event that is represented by the variable $EV. 

The following example replaces two occurrences of and with or.

$NEWSTR = strreplace('and: o tempanda o mandes','and','or',2)

The resulting string is $NEWSTR='or: o tempora o mandes'. The first two occurrences of and have been replaced with or

The following example replaces two occurrences of and with or, skipping the first occurrence.

$NEWSTR = strreplace('and: o tempanda o mandes','and','or',2,1)

The resulting string is $NEWSTR='and: o tempora o mores'. The first occurrence of and is left as it is, and the following two occurrences of and have been replaced with or.

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*