Re: split each element of an array
| From: | Robin Vickery | Date: | Wed, 23 Aug 2000 10:52:41 +0000 |
| Subject: | Re: split each element of an array | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-13137@lists.php.net to get a copy of this message | ||
huntsman@www.nisu.flinders.edu.au (David Robley) writes:
> On 22 Aug, Matthew Luchak wrote:
> >
> > hey all,
> >
> > I would like to split each element of an array by a maximum number of
> > characters.
> > this is what i've got in perl:
> >
> > for (@stuff) { push @newstuff, /(.{1,750})/sg }
> >
> > which keeps the array in sequence and pushes a new element if the element
> > string is longer than 750 characters.
> >
> > I thought it would be easy to do in PHP but it isn't (for me) ;(
> > Could someone point me in the right direction - (array_walk, array_push, am
> > I even getting warm?)
> Erm; not sure exactly how Perl does it in respect of where it sticks the
> new elements, but, and assuming a numerically indexed array, here's some
> pcode....
>
> for each element of $stuff do
> if len(element) < $magicnum
> newarray[] = element
> else
> use substr to break element into magicnum sized chunks
> assign chunks to newarray
> endif
> done
>
> Which leaves a bit to the imagination but hopefully gives you some
> direction. You're right, it's not as compact as the Perl.
>
$newstuff = array();
foreach( $stuff as $element)
{
preg_match_all( "/(.{1,$750})/", $element, $match );
$newstuff = array_merge( $newstuff, $match[1] );
}
--
Robin Vickery...............................................
Planet-Three, 3A West Point, Warple Way, London, W3 0RG, UK
Email: robin@planet-three.net Phone: +44 (0)870 729 5444