note 98428 added to function.str-split

From: Date: Tue, 15 Jun 2010 15:53:24 +0000
Subject: note 98428 added to function.str-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-170140@lists.php.net to get a copy of this message
I needed a function that could split a string from the end with any left over chunk being at the beginning of the array (the beginning of the string). <?php function str_rsplit($str, $sz) { // splits a string "starting" at the end, so any left over (small chunk) is at the beginning of the array. if ( !$sz ) { return false; } if ( $sz > 0 ) { return str_split($str,$sz); } // normal split $l = strlen($str); $sz = min(-$sz,$l); $mod = $l % $sz; if ( !$mod ) { return str_split($str,$sz); } // even/max-length split // split return array_merge(array(substr($str,0,$mod)), str_split(substr($str,$mod),$sz)); } $str = 'aAbBcCdDeEfFg'; str_split($str,5); // return: {'aAbBc','CdDeE','fFg'} str_rsplit($str,5); // return: {'aAbBc','CdDeE','fFg'} str_rsplit($str,-5); // return: {'aAb','BcCdD','eEfFg'} ?> ---- Server IP: 69.147.83.197 Probable Submitter: 74.127.165.244 ---- Manual Page -- http://www.php.net/manual/en/function.str-split.php Edit -- https://master.php.net/note/edit/98428 Del: integrated -- https://master.php.net/note/delete/98428/integrated Del: useless -- https://master.php.net/note/delete/98428/useless Del: bad code -- https://master.php.net/note/delete/98428/bad+code Del: spam -- https://master.php.net/note/delete/98428/spam Del: non-english -- https://master.php.net/note/delete/98428/non-english Del: in docs -- https://master.php.net/note/delete/98428/in+docs Del: other reasons-- https://master.php.net/note/delete/98428 Reject -- https://master.php.net/note/reject/98428 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170140) next »