Hi
Is there any PHP string function I can use to split a string into blocks of 20 characters? Say for instance I had ********************####################(((((((((((((((;;;;;;;;;;;;;;;;;;;;
This would split the string into blocks of 20 characters and return each in an array?
Many thanks
Mark
Splitting a string into blocks
Moderator: General Moderators
-
hokiecsgrad
- Forum Newbie
- Posts: 17
- Joined: Fri Oct 22, 2004 2:55 pm
There's no function that I know of that will do what you want, but one shouldn't be too hard to write:
Code: Select all
<?php
function splitString( $str, $numChars ) {
$strArray = array();
while ( strlen( $str ) > 0 ) {
array_push( $strArray, substr( $str, 0, $numChars ) );
$str = substr( $str, $numChars );
}
return $strArray;
}
$myArray = array();
$myString = "********************####################(((((((((((((((;;;;;;;;;;;;;;;;;;;; ";
$myArray = splitString( $myString, 20 );
echo "<pre>"; print_r( $myArray ); echo "</pre>";
?>[php_man]substr[/php_man]
would go like
would go like
Code: Select all
function splitter($sentence, $length)
{
$parts = array();
while (strlen($sentence) > $length)
{
$parts[] = substr($sentence, 0, $length);
$sentence = substr($sentence, $length);
}
$parts[] = $sentence;
return $parts;
}