note 45071 added to function.str-split

From: Date: Wed, 25 Aug 2004 01:42:47 +0000
Subject: note 45071 added to function.str-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-75239@lists.php.net to get a copy of this message
I realized that when the split length was longer than the string length or the split length was below 1 the str_split() function in PHP5 returns the string as the first entry of the array rather than returning the string as a string like my revision to Rick's PHP does. This should resolve that: <?php function str_split($str, $len=1) { $input_len = strlen($str); if ($len < 1 or $len > $input_len) return array($string); $str_ary = array(); // don't use substr unless you have to if($len == 1) for($cursor = 0; $cursor < $input_len; $cursor++) $str_ary[] = $str{$cursor}; else for ($cursor = 0; $cursor < $input_len; $cursor += $len) $str_ary[] = substr($str, $offset, $len); return $str_ary; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.str-split.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45071 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45071&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45071&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#75239) next »