473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Splitting a string into three parts

I am making a website for a newspaper, and I am having difficulty
figuring out how to take a string (the body of an article) and break
it up into three new strings so that I can display them in the
traditional newspaper column format.

For instance, say the string $articleBody contains a 600 word article.
What I want to do is break $articleBody up into three new strings in a
format such as this:

$string1 contains words 0-200 from $articleBody
$string2 contains words 201-400 from $articleBody
$string3 contains words 401-600 from $articleBody

All I have been able to do is truncate the $articleBody string using a
function similar to this:

function word_split($str,$words=200)
{
$arr = preg_split("/[\s]+/", $str,$words+1);
$arr = array_slice($arr,0,$words);
return join(' ',$arr);
}

However, this will only produce the first of three strings.

Any ideas?

Sep 19 '07 #1
4 2510
<comp.lang.php>
<>
<Wed, 19 Sep 2007 19:25:47 -0000>
<11*********************@22g2000hsm.googlegroups.c om>
I am making a website for a newspaper, and I am having difficulty
figuring out how to take a string (the body of an article) and break
it up into three new strings so that I can display them in the
traditional newspaper column format.

For instance, say the string $articleBody contains a 600 word article.
What I want to do is break $articleBody up into three new strings in a
format such as this:

$string1 contains words 0-200 from $articleBody
$string2 contains words 201-400 from $articleBody
$string3 contains words 401-600 from $articleBody
$newtext=wordwrap($slap,200,"\n",1);
--
(c) The Amazing Krustov
Sep 19 '07 #2
On Sep 19, 3:44 pm, Krustov <m...@privacy.netwrote:
<comp.lang.php>
<>
<Wed, 19 Sep 2007 19:25:47 -0000>
<1190229947.157523.58...@22g2000hsm.googlegroups.c om>
I am making a website for a newspaper, and I am having difficulty
figuring out how to take a string (the body of an article) and break
it up into three new strings so that I can display them in the
traditional newspaper column format.
For instance, say the string $articleBody contains a 600 word article.
What I want to do is break $articleBody up into three new strings in a
format such as this:
$string1 contains words 0-200 from $articleBody
$string2 contains words 201-400 from $articleBody
$string3 contains words 401-600 from $articleBody

$newtext=wordwrap($slap,200,"\n",1);

--
(c) The Amazing Krustov
I'm sorry, but that does not do what I described. I need to take one
string and turn it into three separate strings. Thank you for your
quick reply, though.

Sep 19 '07 #3
On 19.09.2007 21:25 te******@gmail.com wrote:
I am making a website for a newspaper, and I am having difficulty
figuring out how to take a string (the body of an article) and break
it up into three new strings so that I can display them in the
traditional newspaper column format.

For instance, say the string $articleBody contains a 600 word article.
What I want to do is break $articleBody up into three new strings in a
format such as this:

$string1 contains words 0-200 from $articleBody
$string2 contains words 201-400 from $articleBody
$string3 contains words 401-600 from $articleBody

All I have been able to do is truncate the $articleBody string using a
function similar to this:

function word_split($str,$words=200)
{
$arr = preg_split("/[\s]+/", $str,$words+1);
$arr = array_slice($arr,0,$words);
return join(' ',$arr);
}

However, this will only produce the first of three strings.

Any ideas?
hi

given a text $text and an integer $num_cols, you can do something like this

<?

# split the text into words
$w = preg_split('/\s+/', $text);

# create $num_cols arrays of words
$w = array_chunk($w, ceil(count($w) / $num_cols));

# convert arrays of words back to strings
foreach($w as $k =$v) $w[$k] = implode(' ', $v);

# $w contains $num_cols strings
?>

hope this helps.
--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Sep 20 '07 #4
On Sep 20, 5:46 am, gosha bine <stereof...@gmail.comwrote:
On 19.09.2007 21:25 techu...@gmail.com wrote:
I am making a website for a newspaper, and I am having difficulty
figuring out how to take a string (the body of an article) and break
it up into three new strings so that I can display them in the
traditional newspaper column format.
For instance, say the string $articleBody contains a 600 word article.
What I want to do is break $articleBody up into three new strings in a
format such as this:
$string1 contains words 0-200 from $articleBody
$string2 contains words 201-400 from $articleBody
$string3 contains words 401-600 from $articleBody
All I have been able to do is truncate the $articleBody string using a
function similar to this:
function word_split($str,$words=200)
{
$arr = preg_split("/[\s]+/", $str,$words+1);
$arr = array_slice($arr,0,$words);
return join(' ',$arr);
}
However, this will only produce the first of three strings.
Any ideas?

hi

given a text $text and an integer $num_cols, you can do something like this

<?

# split the text into words
$w = preg_split('/\s+/', $text);

# create $num_cols arrays of words
$w = array_chunk($w, ceil(count($w) / $num_cols));

# convert arrays of words back to strings
foreach($w as $k =$v) $w[$k] = implode(' ', $v);

# $w contains $num_cols strings

?>

hope this helps.

--
gosha bine

makrell ~http://www.tagarga.com/blok/makrell
php done right ;)http://code.google.com/p/pihipi
That did the exact trick! ...and it's a very simple solution, too.
Thanks a million, you're a lifesaver gosha bine.

Sep 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sandman | last post by:
I am splitting a text block into paragraphs, to be able to add images and stuff like that to a specific paragraph in a content management system. Well, right now I'm splittin on two or more...
4
by: The Facilitator | last post by:
Well, Outlook seems to be making my life more and more challenging. Categories are added in one field and are delimmited via comma. Have three questions on this one... The first is right now...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
4
by: yogi_bear_79 | last post by:
I have a simple string (i.e. February 27, 2008) that I need to split into three parts. The month, day, and year. Splitting into a string array would work, and I could convert day and years to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.