XtScript Function: str_replace and str_ireplace
Find all occurrences of a case-sensitive, or a case insensitive sub-string within a string and replace them with another sub-string.
If you want to do search-and-replace job, you can do it using two of XtScript's built in function str_replace and str_ireplace.
str_replace is an XtScript function that will search for a substring in search argument and replace it with the given replace argument, CASE-SENSITIVELY.
If you want the function to do the search and replace as CASE-INSENSITIVE, then you can use str_ireplace function instead.
XtScript str_replace and str_ireplace function basic syntax:
<!--parser:xtscript--> # "Search and replace" case-sensitively call str_replace $subject=...; $search=...; $replace=... # Doing case-insensitive "search and replace" call str_ireplace $subject=...; $search=...; $replace... <!--/parser:xtscript-->
Learn XtScript str_replace and str_ireplace function from examples
Do a case-sensitive search and replace operation using XtScript str_replace function.
- Code:<!--parser:xtscript--> # Assigning space into variable using chr function so we can use space as the last character in print command var $spa = call chr $val=32 var $string = I love xtgem and the xtgem script! print Original: $string <br/> print Replaced:$spa call str_replace $subject=$string; $search=xtgem; $replace=<b>server side</b> <!--/parser:xtscript-->- Result:
Original: I love xtgem and the xtgem script!
Replaced: I love server side and the server side script!
Replaced: I love server side and the server side script!
Do a case-insensitive search and replace operation using XtScript str_ireplace function
- Code:<!--parser:xtscript--> var $string = I hope and i think everyone will be ok. var $sea = I var $rep = <u>WE</u> var $new_string = call str_ireplace $subject=$string; $search=$sea; $replace=$rep print Original: $string <br/> print Replaced: $new_string <!--/parser:xtscript-->- Result:
Original: I hope and i think everyone will be ok.
Replaced: WE hope and WE thWEnk everyone wWEll be ok.
Replaced: WE hope and WE thWEnk everyone wWEll be ok.
Last edited on