Re: split each element of an array
| From: | David Robley | Date: | Wed, 23 Aug 2000 06:34:04 +0000 |
| Subject: | Re: split each element of an array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-13092@lists.php.net to get a copy of this message | ||
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?)
>
>
> Matthew Luchak
> Senior Developer
> Special Projects
> Mamma.com
> http://www.62662.com
> http://www.Mamma.com
> tel: 514-908-4336
> matthew@mamma.com
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.
Cheers
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA