Core Function StrrChr
StrChr( <haystack>, <char>, <start>, <count> )
Contents |
Description
Locate first occurrence of character in string.
Returns the position to the first occurrence of character in the string.
The terminating null-character is considered part of the string. Therefore, it can also be located in order to retrieve the length of the string.
Parameters
haystack
The string to search in.
char
Character to be located.
start
Optional; Start position to begin searching the haystack from.
If the start is a negative value the character position will work backwards from the length of the string.
Default: 0
count
Optional; The number of characters to search. By default the entire remainder of the string.
If count 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, NULL will be returned.
Return Value
The position to the first occurrence of character in the haystack.
If the character is not found, the function returns a null.
Remarks
None.
Example
$str = "This is a sample string"; $pch = strrchr($str,'s'); printf ("Last occurence of 's' found at %d\n", $pch); return 0; // Prints // Last occurrence of 's' found at 17
Example of returning the rest of the string after a match
$str = "This is a sample string"; $pch = strrchr($str,'s'); printf ("Last occurence of 's' found at %d\n", $pch); printf ("Rest of the string after match is %s\n", substr($str, $pch+1)); return 0; // Prints // Last occurence of 's' found at 17 // Rest of the string after match is tring
Example of using start parameter
$str = "This is a sample string"; $pch = strrchr($str,'s', 5); printf ("Last occurence of 's' found at %d\n", $pch); printf ("Rest of the string after match is %s\n", substr($str, $pch+1)); return 0; // Prints // Last occurence of 's' found at 17 // Rest of the string after match is tring
Example of using negative start parameter
$str = "This is a sample string"; $pch = strrchr($str,'s', -6); printf ("Last occurence of 's' found at %d\n", $pch); printf ("Rest of the string after match is %s\n", substr($str, $pch+1)); return 0; // Prints // Last occurence of 's' found at 17 // Rest of the string after match is tring