note 56607 deleted from function.str-split by aidan
| From: | aidan@php.net | Date: | Sat, 04 Feb 2006 07:14:06 +0000 |
| Subject: | note 56607 deleted from function.str-split by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-102963@lists.php.net to get a copy of this message | ||
Note Submitter: jbricci at ya-right dot com
----
Here a way to do it using * unpack() *
1. get the size of the string.
2. divide the string length by the character length to split the string at.
3. then build the unpack() to do the multi sub string.
4. use array_combine (because unpack() starts at array element 1)
5. return the split string as a array!
<?
// string to split
$text = 'The cat in the hat. Green eggs and ham. Sam I am.';
// split string at number of characters
$split = 20;
// run it...
$out = split_it ( $text, $split );
print_r ( $out );
function split_it ( $str, $max )
{
$r = strlen ( $str );
if ( $r <= $max )
{
return ( array ( $str ) );
}
$s = array ( 0 );
$t = '';
$u = $r / $max;
$v = '';
if ( strpos ( $u, '.' ) !== false )
{
$p = explode ( '.', $u );
$u = $p[0];
$v = 'A' . '* ' . ( $u + 1 ) . '/';
}
for ( $i = 1; $i <= $u; $i++ )
{
$t .= 'A' . $max . ' ' . $i . '/';
$s[] = $i;
}
$t .= $v;
return ( array_combine ( $s, unpack ( $t, $str ) ) );
}
?>