note 81866 deleted from function.substr by cmb
| From: | cmb@php.net | Date: | Sun, 27 Oct 2019 09:07:36 +0000 |
| Subject: | note 81866 deleted from function.substr by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212825@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
Split a string to an array of strings specified by an array of lengths:
<?php
function split_by_lengths($inString, $arrayLengths)
{
$output = array();
foreach ($arrayLengths as $oneLength)
{
$output[] = substr($inString, 0, $oneLength);
$inString = substr($inString, $oneLength);
}
return ($output);
}
?>
split_by_lengths('teststringtestteststring', array(4,6,4,4,6)) returns:
array('test','string','test','test','string')
Don't use it on user input without some error handling!