note 56607 added to function.str-split
| From: | jbricci at ya-right dot com | Date: | Fri, 09 Sep 2005 05:13:29 +0000 |
| Subject: | note 56607 added to function.str-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94933@lists.php.net to get a copy of this message | ||
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 ) ) );
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.str-split.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56607
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56607&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56607&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56607&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56607&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56607&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56607&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56607&report=yes
Search -- http://master.php.net/manage/user-notes.php