Core Function StrrChr
From Sputnik Wiki
(Difference between revisions)
UberFoX (Talk | contribs)
(Created page with "<pre> StrChr( <haystack>, <char>, <start> ) </pre> === Description === Locate first occurrence of character in string. Returns the position to the first occurrence of characte...")
Newer edit →
(Created page with "<pre> StrChr( <haystack>, <char>, <start> ) </pre> === Description === Locate first occurrence of character in string. Returns the position to the first occurrence of characte...")
Newer edit →
Revision as of 10:19, 4 September 2013
StrChr( <haystack>, <char>, <start> )
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.
Can be negative this will cause it to start from haystack length - abs(start) instead.
Default: 0
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