str_split
CONVERT a particular STRING to an ARRAY.
If the optional $split_length is specified, the returned array will be broken down into chunks with each being $split_length in length, otherwise each chunk will be one character in length.
If $split_length < 1, this function returns FALSE with an additional WARNING error.
If $split_length is greater than $string the entire STRING is returned as an ARRAY of a single element.
<?php
arr str_split ( str $string [, int $split_length = 1 ] )
where,
$string = The input STRING to convert to
$split_length = The maximum length of the chunk
?>
$string
The input STRING to convert to ARRAY.
$split_length
The maximum length of the each chunk.
EXERCISE
<?php
$strspl01 = 'ALEA JACTA EST';
$chunk01 = 5;
// MAXIMUM, 5 CHARACTERS for each chunk
$arrspl01 = str_split($strspl01, $chunk01);
foreach($arrspl01 as $spl01 => $sp01)
{
echo $spl01 . ' => ' . $sp01 . '<br><br>';
}
echo '<pre>';
var_dump($arrspl01);
echo '</pre>';
?>
EXERCISE
<?php
$strspl02 = 'ALEA JACTA EST';
$chunk02 = 1;
// MAXIMUM, 1 CHARACTER for each chunk
$arrspl02 = str_split($strspl02, $chunk02);
foreach($arrspl02 as $spl02 => $sp02)
{
echo $spl02 . ' => ' . $sp02 . '<br>';
}
echo '<pre>';
var_dump($arrspl02);
echo '</pre>';
?>
EXERCISE
<?php
$strspl03 = 'ALEA JACTA EST';
$chunk03 = 20;
// This value exceeds the length
// of the STRING to check
$arrspl03 = str_split($strspl03, $chunk03);
foreach($arrspl03 as $spl03 => $sp03)
{
echo $spl03 . ' =>' . $sp03 . '<br>';
}
echo '<pre>';
var_dump($arrspl03);
echo '</pre>';
?>
EXERCISE
<?php
$strspl04 = 'ALEA JACTA EST';
$chunk04 = -20;
// This value is certainly less than 1
$arrspl04 = str_split($strspl04, $chunk04);
foreach($arrspl04 as $spl04 => $sp04)
{
echo $spl04 . ' => ' . $sp04 . '<br>';
}
echo '<pre>';
var_dump($arrspl04);
echo '</pre>';
?>
EXERCISE
<?php
echo "Testing str_split() : basic functionality.<br>";
// Initialise all required variables
$str = 'This is basic testcase';
$split_length = 5;
echo "Given string:<br>$str<br><br>";
// Calling str_split() with all possible arguments
echo "<br><br>With all possible arguments:<br><pre>";
var_dump( str_split($str,$split_length) );
// Calling str_split() with default arguments
echo "</pre><br><br>With split_length as default argument:<br><pre>";
var_dump( str_split($str) );
echo "</pre>";
?>
EXERCISE
<?php
/*
* passing different double quoted
* strings as 'str' argument to str_split()
* split_length is set to 7
*/
echo "Testing str_split() : double quoted strings for 'str'.<br><br>";
//Initialize variables
$split_length = 7;
// different values for 'str'
$values = [
"",
//empty
" ",
//space
"1234",
//with only numbers
"simple string",
//regular string
"It's string with quote",
//string containing single quote
"string\tcontains\rwhite space\nchars",
"containing @ # $ % ^ & chars",
"with 1234 numbers",
"with \0 and ".chr(0)."null chars",
//for binary safe
"with multiple space char",
"Testing invalid \k and \m escape char",
"to check with \\n and \\t"
//ignoring \n and \t results
];
echo "Given:<br>";
var_dump($values);
echo '<br><br>';
// loop through each element of
// $values for 'str' argument
for($count = 0; $count < count($values); $count++) {
echo "<br><br>Iteration: ".($count+1)."<br>";
echo "After:<br>str_split('$values[$count]', $split_length)<br>";
var_dump( str_split($values[$count], $split_length) );
echo '<br>';
}
?>
EXERCISE
<?php
/*
* passing different single quoted strings
* as 'str' argument to str_split()
* split_length is set to 5
*/
echo "Testing str_split() : single quoted strings for 'str'.<br><br>";
//Initialize variables
$split_length = 5;
// different values for 'str'
$values = array(
'',
//empty
' ',
//space
'1234',
//with only numbers
'simple string',
//regular string
'It\'s string with quote',
//string containing single quote
'string\tcontains\rwhite space\nchars',
'containing @ # $ % ^ & chars',
'with 1234 numbers',
'with \0 and ".chr(0)."null chars',
//for binary safe
'with multiple space char',
'Testing invalid \k and \m escape char',
'to check with \\n and \\t'
//ignoring \n and \t results
);
echo "Given:<br>";
foreach($values as $k => $v)
echo "[ $k ] => \"$v\"<br>";
// loop through each element of
// $values for 'str' argument
for($count = 0; $count < count($values); $count++) {
echo "<br><br><br>Iteration: ".($count+1)."<br>";
echo "After:<br>str_split('$values[$count]', $split_length)<br>";
var_dump( str_split($values[$count], $split_length) );
}
?>
EXERCISE
<?php
/*
* Passing different heredoc strings
* as 'str' argument to the str_split()
* with 'split_length' 10
*/
echo "Testing str_split() : heredoc strings as 'str' argument.<br>";
// Initializing required variables
$split_length = 10;
// Null heredoc string
$heredoc_null = <<<EOT1
EOT1;
// heredoc string with single character
$heredoc_char = <<<EOT2
a
EOT2;
// simple heredoc string
$heredoc_str = <<<EOT3
This is simple heredoc string
EOT3;
// heredoc with special characters
$heredoc_spchar = <<<EOT4
This checks heredoc with $, %, &, chars
EOT4;
// blank heredoc string
$heredoc_blank = <<<EOT5
EOT5;
// heredoc with different white space characters
$heredoc_escchar = <<<EOT6
This checks\t str_split()\nEscape\rchars
EOT6;
// heredoc with multiline
$heredoc_multiline= <<<EOT7
This is to check str_split
function with multiline
heredoc
EOT7;
// heredoc with quotes and slashes
$heredoc_quote_slash = <<<EOT8
"To check " in heredoc"
I'm sure it'll work also with \
which is single slash
EOT8;
//different heredoc strings for 'str'
$heredoc_array = array(
$heredoc_null,
$heredoc_blank,
$heredoc_char,
$heredoc_str,
$heredoc_multiline,
$heredoc_spchar,
$heredoc_escchar,
$heredoc_quote_slash
);
// loop through each element of
// the 'heredoc_array' for 'str'
$count = 0;
foreach($heredoc_array as $str) {
echo "<br><br>Iteration: ".($count+1). "<br>";
echo "<br>str_split('$str', $split_length):<br><br>";
var_dump( str_split($str, $split_length) );
$count++;
};
?>
EXERCISE
<?php
/*
* passing different integer values
* for 'split_length' argument to str_split()
*/
echo "Testing str_split() :
different integer values for 'split_length'.<br>";
//Initialise variables
$str = 'This is a string with 123 & escape char \t';
//different values for 'split_length'
$values = [
0,
1,
-123, //negative integer
0234, //octal number
0x1A, //hexadecimal number
2147483647, //max positive integer number
-2147483648, //min negative integer
];
//loop through each element of $values for 'split_length'
for($count = 0; $count < count($values); $count++) {
echo "<br><br>Iteration: ".($count + 1)."<br>";
try {
var_dump( str_split($str, $values[$count]) );
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
}
?>
EXERCISE
<?php
/*
* passing different integer values
* for 'split_length' and heredoc string
* as 'str' argument to str_split()
*/
echo "Testing str_split() :
different integer values for
'split_length' with
heredoc 'str'.<br>";
//Initialise variables
$str = <<<EOT
string with 123,escape char \t.
EOT;
//different values for 'split_length'
$values = array (
0,
1,
-123, //negative integer
0234, //octal number
0x1A, //hexadecimal number
2147483647, //max positive integer number
-2147483648, //min negative integer
);
// loop through each element
// of $values for 'split_length'
for($count = 0; $count < count($values); $count++) {
echo "<br><br>Iteration: ".($count + 1)."<br>";
try {
var_dump( str_split($str, $values[$count]) );
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
}
?>