<?xml version="1.0" encoding="windows-1252"?>
<node id="845408" title="Re: substr question" created="2010-06-18 12:50:17" updated="2010-06-18 12:50:17">
<type id="11">
note</type>
<author id="590906">
linuxer</author>
<data>
<field name="doctext">
&lt;p&gt;I modified your problem definition into:
&lt;p&gt;How to split a string into several substrings (of a given maximum length) without breaking a word when splitting.

&lt;p&gt;I found this (quickly hacked) solution:

&lt;code&gt;
#! /opt/perl/bin/perl
use strict;
use warnings;

my $text = "Hello, I am a perl/mysql programmer, but am self taught so I do not know all the awesome features Perl has, however, I am ok at it though, I guess.";

sub split_at {
	my ( $text, $len ) = @_;
	my @result;

	my @parts = split /[ ]/, $text;

	my $short = shift @parts;
	while ( @parts ) {
		if ( length($short) + length($parts[0]) &lt; $len ) {
			$short = join( ' ', $short, shift @parts );
		}
		else {
			push @result, $short;
			$short = shift @parts;
		}
	}

	push @result, $short;

	return @result;
}


{
	local $, = local $\ = $/;
	print split_at( $text, 100 );

	# beware
	print split_at( 'a'x110, 100 );
}

&lt;/code&gt;

&lt;p&gt;Limitation: If the given string doesn't contain any whitespace (&lt;c&gt;\x20&lt;/c&gt;), it won't split at all.

&lt;p&gt;If you just want/need the first substring, just use the first returned element.

&lt;small&gt;
&lt;p&gt;Updates:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;cleaned up code&lt;/li&gt;
&lt;li&gt;fixed comparison; thanks, [rowdog]&lt;/li&gt;
&lt;/ul&gt;
&lt;/small&gt;</field>
<field name="root_node">
845399</field>
<field name="parent_node">
845399</field>
</data>
</node>
