note 102892 added to function.str-split
| From: | jaimthorn at yahoo dot com | Date: | Sun, 13 Mar 2011 01:57:26 +0000 |
| Subject: | note 102892 added to function.str-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177830@lists.php.net to get a copy of this message | ||
A small function that splits a string into variable-length segments. Easier than substr-ing a
string to bits. :-)
/**
* Splits a string, based on the integer offsets supplied as variable argument list.
*
* @param string
* @param VARARG ints
* @return array
*/
function splitByOffsets( $string )
{
$retval = array();
$args = func_get_args();
array_shift( $args ); // Shift $string out.
$start = 0;
foreach( $args as $arg ) {
$retval[] = substr( $string, $start, $arg );
$start += $arg;
}
if( strlen( $string ) > $start ) {
$retval[] = substr( $string, $start );
}
return $retval;
}
// Split the string in chunks of 4, 2, and 2 characters, plus whatever is left.
$date = splitByOffsets( "20110313 is today's date", 4, 2, 2 );
echo 'Today is: ', $date[ 2 ], '-', $date[ 1 ], '-', $date[ 0 ]; //
Today is: 13-03-2011
echo 'Left is: ', $date[ 3 ]; // Left is: is
today's date
----
Server IP: 83.137.20.135
Probable Submitter: 77.251.14.61
----
Manual Page -- http://www.php.net/manual/en/function.str-split.php
Edit -- https://master.php.net/note/edit/102892
Del: integrated -- https://master.php.net/note/delete/102892/integrated
Del: useless -- https://master.php.net/note/delete/102892/useless
Del: bad code -- https://master.php.net/note/delete/102892/bad+code
Del: spam -- https://master.php.net/note/delete/102892/spam
Del: non-english -- https://master.php.net/note/delete/102892/non-english
Del: in docs -- https://master.php.net/note/delete/102892/in+docs
Del: other reasons-- https://master.php.net/note/delete/102892
Reject -- https://master.php.net/note/reject/102892
Search -- https://master.php.net/manage/user-notes.php