Core Function InStrRev
InStrRev( <string>, <substirng>, <casesense>, <occurrence>, <start>, <length> )
Contents |
Description
Same as InStr() but searches from right to left instead of left to right.
Parameters
string
The string to evaluate.
substring
The substring to search for.
ignoreCase
Optional; Flag to indicate if the operations should be case insensitive.
true = not case sensitive
false = case sensitive
Default: true
occurrence
Optional; Which occurrence of the substring to find in the string. Use a negative occurrence to search from the right side.
The default value is 1 (finds first occurrence).
start
Optional; The starting position of the search.
OR
If the start is a negative value the character position will work backwards from the length of the string.
Default: 0
length
Optional; The number of characters to search. By default the entire remainder of the string.
If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, -1 will be returned.
Return Value
Success: Returns the position of the substring.
Failure: Returns -1.
Remarks
The first character position is 0.
You might think you set the start to say 10 (in a string of size 20) then expect InStrRev() to search backwards from that point but you will be mistaken as InStrRev() will search from the END of the string to the start you mentioned (10). To solve this issue you should set the start to 0 and the length to 10 then it will correctly search from position 10 to the start (0).
This is because InStrRev() works similar to InStr() and SubStr() in how it handles start/length in that the start+length defines WHAT will be searched (the entire search content is defined in the start+length).
Example
$TestString = "the quick brown fox jumps over the lazy dog"; $TestNumber = InStrRev($TestString, "the"); // 31 say $TestNumber . " | " . substr($TestString, $TestNumber, 3); $TestNumber = InStrRev($TestString, "the", true, 1, 0, 16); // 0 say $TestNumber . " | " . substr($TestString, $TestNumber, 3); // PRINTS // 31 | the // 0 | the