Core Function Strpbrk

From Sputnik Wiki
Revision as of 20:38, 3 September 2013 by UberFoX (Talk | contribs)
Jump to: navigation, search
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'));
// Prints
// Vowels in 'This is a sample string': i i a a e i
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox