Categories
Coding

XtScript Function: strrpos and strripos

XtScript strrpos and strripos is XtScript function that will search and return the position of the last occurrence of a matched substring.

The difference between strrpos and strripos is that strrpos will search the substring as CASE-SENSITIVE, while strripos as CASE-INSENSITIVE.

If you want the function to search and return the position of the first occurrence of a substring, you can use XtScript strpos and stripos function instead.

XtScript strrpos and strripos function basic syntax:

<!--parser:xtscript-->
  # Find the last occurrence of a case-sensitive substring and return the position
  call strrpos $haystack=...; $needle=...; $offset=...
  # Find the last occurrence of a case-insensitive substring and return the position
  call strripos $haystack=...; $needle=...; $offset=...
<!--/parser:xtscript-->

Available arguments in XtScript strrpos and strripos function:

XtScript strrpos and strripos function arguments
Argument Value Mandatory Explanation
$haystack string Required Specify the main string where the substring will be searched in.
$needle string Required Specify the substring that searched for.
$offset number Optional Specify the index of a character the search will be started from. Negative value will make the function work started from the number from the end of string, making it has backward behaviour.

Learn XtScript strrpos and strripos function from examples

Search and return the position of the last occurrence of a case-sensitive substring using XtScript strrpos function.

  <!--parser:xtscript-->
    var $string = Adam Jordan | Michael Jordan | Angela Jordan | michael Heissenberg
    var $fname = Jordan
    var $name = Michael
    print Names: $string <br/><br/>
    print "$fname" family name found on position: <b>
    call strrpos $haystack=$string; $needle=$fname
    print </b> <br/><br/>
    print "$name" found on position: <b>
    call strrpos $haystack=$string; $needle=$name
    print </b>
  <!--/parser:xtscript-->
  
Names: Adam Jordan | Michael Jordan | Angela Jordan | michael Heissenberg

“Jordan” family name found on position: 38

“Michael” found on position: 14

Remember that index on programming like Javascript, PHP, and also XtScript is always started at 0.

Search and return the position of the last occurrence of a case-insensitive substring using XtScript strripos function.

  <!--parser:xtscript-->
    var $string = Go Goes Going GOal
    var $srch = go
    print String: $string <br/><br/>
    print "$srch" found at position: <b>
    call strripos $haystack=$string; $needle=$srch;
    print </b> <br/><br/>
    print With \$offset=-5, "$srch" found at position: <b>
    call strripos $haystack=$string; $needle=$srch; $offset=-5
    print </b>
  <!--/parser:xtscript-->
  
String: Go Goes Going GOal

“go” found at position: 14

With $offset=-5, “go” found at position: 8

By SiDar

Webmaster Assistant and Editor at Indotechboy.com.

SiDar enjoying himself listening J-pop genre music and playing games, especially old school games. Sometimes he also found watching anime and reading manga are interesting when his fellow author recommend him some good titles.

Leave a Reply

Your email address will not be published. Required fields are marked *