note 112064 added to function.str-split

From: Date: Sun, 28 Apr 2013 18:48:45 +0000
Subject: note 112064 added to function.str-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-194649@lists.php.net to get a copy of this message
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>&nbsp;</td>"; } else { $html .= "<td>$char</td>"; } } $html .= "</tr>\n"; } $html .= "</table>\n"; echo $html; ?> ---- Server IP: 69.147.83.199 Probable Submitter: 99.108.10.120 ---- Manual Page -- http://php.net/manual/en/function.str-split.php Edit -- https://master.php.net/note/edit/112064 Del: integrated -- https://master.php.net/note/delete/112064/integrated Del: useless -- https://master.php.net/note/delete/112064/useless Del: bad code -- https://master.php.net/note/delete/112064/bad+code Del: spam -- https://master.php.net/note/delete/112064/spam Del: non-english -- https://master.php.net/note/delete/112064/non-english Del: in docs -- https://master.php.net/note/delete/112064/in+docs Del: other reasons-- https://master.php.net/note/delete/112064 Reject -- https://master.php.net/note/reject/112064 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194649) next »