strspn
FINDS the length of the initial segment of a string consisting entirely of characters contained within a given mask.
When $start is set and is non-negative, the returned $length is counted starting from this position, not from the beginning of $subject, however, if negative the $length is counted from the end of the $subject.
If $length is given and is non-negative, then $subject will be examined for $length characters after the starting position.
If $length is given and is negative, then $subject will be examined from the starting position up to $length characters from the end of $subject.
This function is binary-safe
<?php
int strspn ( str $subject ,
str $mask [,
int $start [,
int $length ]] )
where,
$subject = The STRING to be examined
$mask = The list of allowable characters
$start = The start position in $subject to start the searching
$length = The length of the segment from $subject to examine.
?>
$subject
The STRING to to examine.
$mask
The list of allowable characters.
$start
The position on the $subject to start the seaching.
$length
The length of the segment from $subject to examine.
EXERCISE
<?php
$str01 = 'Whoever laughs last, GENERALLY, laughs best!';
$mask01 = 'GEYNLLRAabcd';
$start01 = mt_rand(-strlen($str01), strlen($str01));
$length01 = mt_rand(-strlen($str01), strlen($str01));
echo 'start = ' . $start01 . '<br>length = ' .
$length01 . '<br>mask = "' .
$mask01 . '"<br><br>';
$scount01 = strspn($str01, $mask01, $start01, $length01);
echo 'In the present situation "' . $mask01 .
'" has ' . $scount01 .
' characters in: <br><br>"' .
$str01 . '"<br><br>';
?>
EXERCISE
<?php
$a = "22222222aaaa bbb1111 cccc";
$b = "1234";
echo 'Given:<br><br>';
var_dump($a);
echo '<br>';
var_dump($b);
echo "<br><br><br>After: strspn($a,$b)<br>";
var_dump(strspn($a,$b));
echo "<br><br><br>After: strspn($a,$b,2)<br>";
var_dump(strspn($a,$b,2));
echo "<br><br><br>After: strspn($a,$b,2,3)<br>";
var_dump(strspn($a,$b,2,3));
?>
EXERCISE
<?php
/*
* Testing strspn() : basic functionality
*/
echo "Testing strspn() : basic functionality.<br>";
// Initialise all required variables
$str = "this is the test string";
$mask = "htes ";
$start = 8;
$len = 12;
// Calling strspn() with all possible arguments
var_dump( strspn($str, $mask, $start, $len) );
echo '<br>';
// Calling strspn() with three arguments
// and default len argument
var_dump( strspn($str, $mask, $start) );
echo '<br>';
// Calling strspn() with default arguments
var_dump( strspn($str, $mask) );
?>
EXERCISE
<?php
/* Prototype :
* proto int strspn(string str, string mask [, ...])
* Description:
* Finds length of initial segment consisting
* entirely of characters found in mask.
If start or/and length is provided works like
strspn(substr($s,$start,$len),$good_chars)
* Source code: ext/standard/string.c
* Alias to functions: none
*/
/*
* Testing strspn() :
* with different heredoc strings as str argument
*/
echo "Testing strspn() : with heredoc strings.<br>";
// initialing required variables
// defining different heredoc strings
$empty_heredoc = <<<EOT
EOT;
$heredoc_with_newline = <<<EOT
\n
EOT;
$heredoc_with_characters = <<<EOT
first line of heredoc string
second line of heredoc string
third line of heredocstring
EOT;
$heredoc_with_newline_and_tabs = <<<EOT
hello\tworld\nhello\nworld\n
EOT;
$heredoc_with_alphanumerics = <<<EOT
hello123world456
1234hello\t1234
EOT;
$heredoc_with_embedded_nulls = <<<EOT
hello\0world\0hello
\0hello\0
EOT;
$heredoc_with_hexa_octal = <<<EOT
hello\0\100\xaaworld\0hello
\0hello\0
EOT;
$heredoc_strings = [
$empty_heredoc,
$heredoc_with_newline,
$heredoc_with_characters,
$heredoc_with_newline_and_tabs,
$heredoc_with_alphanumerics,
$heredoc_with_embedded_nulls,
$heredoc_with_hexa_octal
];
$mask = "sfth12\ne34l567890\0\xaa\100o";
// loop through each element of the array for str argument
foreach($heredoc_strings as $str) {
echo "<br><br>Iteration with str value as \"$str\" <br>";
var_dump( strspn($str,$mask) );
// with default start and len values
};
?>
EXERCISE
<?php
/*
* Testing strspn() :
* with heredoc string, varying mask and
* default start and len arguments
*/
echo "Testing strspn() :
with different mask strings.<br>";
// initialing required variables
// defining different heredoc strings
$empty_heredoc = <<<EOT
EOT;
$heredoc_with_newline = <<<EOT
\n
EOT;
$heredoc_with_characters = <<<EOT
first line of heredoc string
second line of heredoc string
third line of heredocstring
EOT;
$heredoc_with_newline_and_tabs = <<<EOT
hello\tworld\nhello\nworld\n
EOT;
$heredoc_with_alphanumerics = <<<EOT
hello123world456
1234hello\t1234
EOT;
$heredoc_with_embedded_nulls = <<<EOT
hello\0world\0hello
\0hello\0
EOT;
$heredoc_with_hexa_octal = <<<EOT
hello\0\100\xaaworld\0hello
\0hello\0
EOT;
// defining array of different heredoc strings
$heredoc_strings = array(
$empty_heredoc,
$heredoc_with_newline,
$heredoc_with_characters,
$heredoc_with_newline_and_tabs,
$heredoc_with_alphanumerics,
$heredoc_with_embedded_nulls,
$heredoc_with_hexa_octal
);
// defining array of different mask strings
$mask_array = array(
"",
'',
"fh\ne\trlsti \l",
'fieh\n\trlsti \l',
"\t",
"lt\ ",
'l\t',
"fl\t\eh ",
"l \te",
"lf\the\i\100\xaa"
);
// loop through each element of the array
// for different heredoc and mask strings
$count = 1;
foreach($heredoc_strings as $str) {
echo "<br><br>Iteration: $count<br>";
foreach($mask_array as $mask) {
var_dump( strspn($str,$mask) );
// with default start and len value
echo '<br>';
}
$count++;
}
?>
EXERCISE
<?php
/* Prototype :
* proto int strspn(string str, string mask [, ...])
* Description:
* Finds length of initial segment
* consisting entirely of characters found in mask.
If start or/and length is provided works like
strspn(substr($s,$start,$len),$good_chars)
* Source code: ext/standard/string.c
* Alias to functions: none
*/
/*
* Testing strspn() :
* with different strings as str argument
* and default start and len args
*/
echo "Testing strspn() :
with different str and default
start and len args.<br>";
// initialing required variables
// defining different strings
$strings = array(
"",
'',
"\n",
'\n',
"hello\tworld\nhello\nworld\n",
'hello\tworld\nhello\nworld\n',
"1234hello45world\t123",
'1234hello45world\t123',
"hello\0world\012",
'hello\0world\012',
chr(0).chr(0),
chr(0)."hello\0world".chr(0),
chr(0).'hello\0world'.chr(0),
"hello".chr(0)."world",
'hello'.chr(0).'world',
"hello\0\100\xaaaworld",
'hello\0\100\xaaaworld'
);
$mask = "sfth12\ne34lw56r78d90\0\xaa\100o";
// loop through each element of the array for str argument
foreach($strings as $str) {
echo "<br><br>Iteration with str value \"$str\"<br>";
//calling strspn() with default arguments
var_dump( strspn($str,$mask) );
};
?>
EXERCISE
<?php
/*
* Testing strspn() :
* with varying mask and default
* start and len arguments
*/
echo "Testing strspn() :
with different mask strings and
default start and len arguments.<br>";
// initialing required variables
// defining different strings
$strings = array(
"",
'',
"\n",
'\n',
"hello\tworld\nhello\nworld\n",
'hello\tworld\nhello\nworld\n',
"1234hello45world\t123",
'1234hello45world\t123',
"hello\0world\012",
'hello\0world\012',
chr(0).chr(0),
chr(0)."hello\0world".chr(0),
chr(0).'hello\0world'.chr(0),
"hello".chr(0)."world",
'hello'.chr(0).'world',
"hello\0\100\xaaaworld",
'hello\0\100\xaaaworld'
);
// define the array of mask strings
$mask_array = array(
"",
'',
"f\n\trelshti \l",
'f\n\trelsthi \l',
"\telh",
"t\ ",
'\telh',
"felh\t\ ",
" \t",
"fhel\t\i\100\xa"
);
// loop through each element of the array
// for mask argument
$count = 1;
foreach($strings as $str) {
echo "<br><br>Iteration: $count<br>";
foreach($mask_array as $mask) {
var_dump( strspn($str,$mask) );
echo '<br>';
}
$count++;
}
?>
EXERCISE
<?php
/*
* Testing strspn() :
* with varying start and default len arguments
*/
echo "Testing strspn() :
with different start and
default len values.<br>";
// initialing required variables
// defining different strings
$strings = array(
"",
'',
"\n",
'\n',
"hello\tworld\nhello\nworld\n",
'hello\tworld\nhello\nworld\n',
"1234hello45world\t123",
'1234hello45world\t123',
"hello\0world\012",
'hello\0world\012',
chr(0).chr(0),
chr(0)."hello\0world".chr(0),
chr(0).'hello\0world'.chr(0),
"hello".chr(0)."world",
'hello'.chr(0).'world',
"hello\0\100\xaaaworld",
'hello\0\100\xaaaworld'
);
// define the array of mask strings
$mask_array = array(
"",
'',
"f\n\trelshti \l",
'f\n\trelsthi \l',
"\telh",
"t\ ",
'\telh',
"felh\t\ ",
" \t",
"fhel\t\i\100\xa"
);
// defining the array for start values
$start_array = array(
0,
1,
2,
-1,
-2,
2147483647,
// max positive integer
-2147483648,
// min negative integer
);
// loop through each element of the arrays
// for str, mask and start argument
$count = 1;
foreach($strings as $str) {
echo "<br><br>Iteration: $count<br>";
foreach($mask_array as $mask) {
foreach($start_array as $start) {
try {
var_dump( strspn($str,$mask,$start) );
echo '<br>';
} catch (ValueError $e) {
echo $e->getMessage(), "<br><br>";
}
}
}
$count++;
}
?>
EXERCISE
<?php
/*
* Testing strspn() :
* with varying start and len arguments
*/
echo "Testing strspn() :
with different start and len values.<br>";
// initialing required variables
// defining different strings
$strings = [
"",
'',
"\n",
'\n',
"hello\tworld\nhello\nworld\n",
"1234hello45world\t123",
"hello\0world\012",
chr(0).chr(0),
chr(0)."hello\0world".chr(0),
"hello".chr(0)."world",
"hello\0\100\xaaaworld",
];
// define the array of mask strings
$mask_array = [
"",
'',
"f\n\trelshti \l",
'f\n\trelsthi \l',
"\telh",
"felh\t\ ",
"fhel\t\i\100\xa"
];
// defining the array for start values
$start_array = [
0,
1,
2,
-1,
2147483647,
// max positive integer
-2147483648,
// min negative integer
];
// defining an array of len values
$len_array = [
0,
1,
2,
-1,
2147483647,
// max positive integer
-2147483648,
// min negative integer
];
// loop through each element of
// the array for len argument
$count = 1;
foreach($strings as $str) {
echo "<br><br>Iteration: $count<br>";
foreach($mask_array as $mask) {
foreach($start_array as $start) {
foreach($len_array as $len) {
try {
var_dump( strspn($str,$mask,$start,$len) );
echo '<br><br>';
} catch (ValueError $e) {
echo $e->getMessage(), "<br>";
}
}
}
}
$count++;
}
?>