Core Function Strpbrk
From Sputnik Wiki
(Difference between revisions)
m (1 revision) |
m (1 revision) |
Revision as of 12:37, 14 June 2015
Strpbrk( <haystack>, <needle>, <start> )
Contents |
Description
Locate a list of possible characters in string and return the position of it.
Parameters
haystack
The string to search in.
needle
The string containing the characters to match.
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
Position of the first occurrence in the haystack of any of the characters that are part of needle.
If none of the characters of needle is present in haystack, a null is returned.
Remarks
None.
Example
my $str = "This is a sample string"; my $key = "aeiou"; printf ("Vowels in '%s': ",$str); my $pch = strpbrk ($str, $key); while ($pch !== NULL) { printf ("%c " , $str[$pch]); $pch = strpbrk( $str, $key, $pch+1 ); } printf ("\n"); return 0; // Prints // Vowels in 'This is a sample string': i i a a e i
You may want to return the string after the match
$text = 'This is a Simple text.'; // this echoes "is is a Simple text." because 'i' is matched first say substr($text, strpbrk($text, 'mi')); // this echoes "Simple text." because chars are case sensitive say substr($text, strpbrk($text, 'S'));
Example of using negative start position
// this echoes ";Cat" since -4 starts from 4 letters from end of the string $text = "Hello;Cat"; say substr($text, strpbrk($text, ';', -4));