note 112064 deleted from function.str-split by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 18:43:34 +0000 |
| Subject: | note 112064 deleted from function.str-split by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218696@lists.php.net to get a copy of this message | ||
Note Submitter: stlawson AT JoyfulEarthTech DOT com
----
The documentation fails to mention what happens when the string length does not divide evenly with
the chunk size. Not sure if the same behavior for all versions of PHP so I offer the following code
to determine this for your installation. On mine [version 5.2.17], the last chunk is an array the
length of the remaining chars.
<?php
$da_string = "When number of chars does't divide evenly";
$len = strlen($da_string);
$chunk_size = 8;
echo "<p> Length of <span
style='font-family:monospace'>$da_string</span>: $len</p>\n";
echo "<p> Chunck size: $chunk_size</p>\n";
$parts = str_split($da_string, $chunk_size);
$html = "<table border='5' cellpadding='3'
cellspacing='4'>\n";
foreach ($parts as $idx=>$part)
{
$html .= "<tr>\n";
$html .= "<td style='font-family:monospace'>$part</td>";
$chars = str_split($part);
foreach ($chars as $char)
{
if ($char === ' ')
{
$html .= "<td> </td>";
}
else
{
$html .= "<td>$char</td>";
}
}
$html .= "</tr>\n";
}
$html .= "</table>\n";
echo $html;
?>