Core Function Strpbrk

From Sputnik Wiki
Jump to: navigation, search
Strpbrk( <haystack>, <needle>, <start>, <count> ) 

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.

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

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));
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox