strcspn 


string apg

FINDS the length of the initial segment of a string consisting entirely of characters not matching with 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 strcspn 
str $subject 
                   
str $mask [, 
                   
int $start [, 
                   
int $length ]] )


where,

$subject The STRING to be examined

$mask 
The STRING containing every disallowed character

$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.



  1 EXERCISE   

<?php

$a 
"22222222aaaa bbb1111 cccc";
$b "1234";

echo 
'Given:<br><br>';
var_dump($a);
echo 
'<br>and<br>';
var_dump($b); 
echo 
"<br><br><br>After: strcspn($a,$b)<br>"
var_dump(strcspn($a,$b));
echo 
"<br><br><br>After: strcspn($a,$b,9)<br>"
var_dump(strcspn($a,$b,9));
echo 
"<br><br><br>After: strcspn($a,$b,9,6)<br>"
var_dump(strcspn($a,$b,9,6));
echo 
'<br><br>';

try {
    
var_dump(strcspn('a''B'12147483647));    
} catch (
ValueError $e) {
    echo 
$e->getMessage(), "<br><br>";
}

?>

  2 EXERCISE   

<?php

/*
* Testing strcspn() : basic functionality
*/

echo "Testing strcspn() : basic functionality.<br>";


// Initialise all required variables
$str "this is the test string";
$mask "es";
$start 15;
$len 3;

// Calling strcspn() with all possible arguments
var_dumpstrcspn($str$mask$start$len) );
echo 
'<br><br>';
// Calling strcspn() with three arguments
var_dumpstrcspn($str$mask$start) );
echo 
'<br><br>';
// Calling strcspn() with default arguments
var_dumpstrcspn($str$mask) );

?>

  3 EXERCISE   

<?php

/* Prototype  : 
 * proto int strcspn(string str, string mask [, ...])
 * Description: 
 * Finds length of initial segment consisting 
 * entirely of characters not found in mask.
          If start or/and length is provided works like
          strcspn(substr($s,$start,$len),$bad_chars)
 * Source code: ext/standard/string.c
 * Alias to functions: none
*/

/*
* Testing strcspn() : 
* with different heredoc strings as str argument
*/

echo "Testing strcspn() : 
                 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 = 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
                   
);

$mask "fth12\ne67890\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_dumpstrcspn($str,$mask) );
      
// with default start and len values
};

?>

  4 EXERCISE   

<?php

/*
* Testing strcspn() : 
* with heredoc string, varying mask 
* and default start and len arguments
*/

echo "Testing strcspn() : 
               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;

$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
                   
];

// defining array of mask strings
$mask_array =[
            
"",
            
'',
            
"\n\trsti \l",
            
'\n\trsti \l',
            
"\t",
            
"t\ ",
            
'\t',
            
"\t\ ",
            
" \t",
                    
"\t\i\100\xaa"
                   
];


// loop through each element of the 
// arrays for string and mask arguments

$count 1;
foreach(
$heredoc_strings as $str) {
  echo 
"<br><br>Iteration: $count<br>";
  foreach(
$mask_array as $mask) {
      
var_dumpstrcspn($str,$mask) ); 
      echo 
'<br>';
      
// with default start and len value
  
}
  
$count++;
}

?>

  5 EXERCISE   

<?php

/* Prototype  : 
 * proto int strcspn(string str, string mask [, ...])
 * Description: 
 * Finds length of initial segment consisting 
 * entirely of characters not found in mask.
        If start or/and length is provided works like 
        strcspn(substr($s,$start,$len),$bad_chars)
 * Source code: ext/standard/string.c
 * Alias to functions: none
*/

/*
* Testing strcspn() : 
* with different strings as str argument 
* and default start and len args
*/

echo "Testing strcspn() : 
           with different str and 
                      default start and len args.<br>"
;

// initialing required variables
// defining different strings

$strings = [
                   
"",
           
'',
           
"\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 "sft\n34lw56r78d90\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 strcspn() with default arguments
      
var_dumpstrcspn($str,$mask) );
};

?>

  6 EXERCISE   

<?php

/*
* Testing strcspn() : 
* with varying mask and default start 
* and len arguments
*/

echo "Testing strcspn() : 
            with different mask strings 
                   and default start and len arguments.<br>"
;

// initialing required variables
$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'
                   
);

// defining array of mask strings
$mask_array = array(
            
"",
            
'',
            
"\n\trsti \l",
            
'\n\trsti \l',
            
"\t",
            
"t\ ",
            
'\t',
            
"\t\ ",
            
" \t",
                    
"\t\i\100\xa"
                   
);


// loop through each element of 
// the array for mask argument
$count 1;
foreach(
$strings as $str) {
  echo 
"<br><br>Itearation: $count<br>";
  foreach(
$mask_array as $mask) {
      
var_dumpstrcspn($str,$mask) );
      echo 
'<br>';
  }
  
$count++;
}

?>

  7 EXERCISE   

<?php


/*
* Testing strcspn() : 
* with varying start and default len arguments
*/

echo "Testing strcspn() : 
          with different start and default len values.<br>"
;

// initialing required variables
// initialing required variables
$strings = [
                   
"",
                   
'',
                   
"\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'
                   
];

// defining array of mask strings
$mask_array = [
                    
"",
                    
'',
                    
"\n\trsti \l",
                    
'\n\trsti \l',
                    
"\t",
                    
"t\ ",
                    
'\t',
                    
"\t\ ",
                    
" \t",
                    
"\t\i\100\xa"
                   
];

//defining array of start values
$start_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 arguments
$count 1;
foreach(
$strings as $str) {
  echo 
"<br><br>Iteration: $count<br>";
  foreach(
$mask_array as $mask) {
    foreach(
$start_array as $start) {
      try {
        
var_dumpstrcspn($str,$mask,$start) );
        echo 
'<br>';
      } catch (
ValueError $e) {
        echo 
'<br>' $e->getMessage(), "<br>";
      }
    }
  }
  
$count++;
}

?>

  8 EXERCISE   

<?php

/*
* Testing strcspn() : 
* with varying start and len arguments
*/

echo "Testing strcspn() : 
           with different start and len values.<br>"
;

// initialing required variables
// initialing required variables
$strings = array(
                   
"",
                   
'',
                   
"\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\xaworld",
                   );

// defining array of mask strings
$mask_array = array(
                    
"",
                    
'',
                    
"\n\trsti \l",
                    
"\t",
                    
"t\ ",
                    
"\t\i\100\xa"
                   
);

//defining array of start values
$start_array = array(
            
0,
            
1,
            
2,
            -
1,
            
2147483647,
            
// max positive integer
            
-2147483648
            
// min negative integer
                   
);

//defining array of len values
$len_array = array(
            
0,
            
1,
            
2,
            -
1,
            
2147483647,
            
// max positive integer
            
-2147483648
            
// min negative integer
                   
);


// loop through each element of the arrays 
// for str,mask,start and len arguments

$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_dumpstrcspn($str,$mask,$start,$len) );
          echo 
'<br>';
        } catch (
ValueError $e) {
          echo 
'<br>' $e->getMessage(), "<br>";
        }
      }
    }
  }
  
$count++;
}

?>