Core Function Strpbrk

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
UberFoX (Talk | contribs)
(Created page with "<pre> Strpbrk( <haystack>, <needle>, <start> ) </pre> === Description === Locate a list of possible characters in string and return the position of it. === Parameters === ==...")
Newer edit →

Revision as of 10:32, 3 September 2013

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.

(Cannot be negative)

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