473,750 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($ar r,0,$words);
return join(' ',$arr);
}

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

Any ideas?

Sep 19 '07 #1
4 2540
<comp.lang.ph p>
<>
<Wed, 19 Sep 2007 19:25:47 -0000>
<11************ *********@22g20 00hsm.googlegro ups.com>
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=wordwr ap($slap,200,"\ n",1);
--
(c) The Amazing Krustov
Sep 19 '07 #2
On Sep 19, 3:44 pm, Krustov <m...@privacy.n etwrote:
<comp.lang.ph p>
<>
<Wed, 19 Sep 2007 19:25:47 -0000>
<1190229947.157 523.58...@22g20 00hsm.googlegro ups.com>
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=wordwr ap($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($ar r,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...@gma il.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($ar r,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
3304
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 newlines, so this text block: Hello, my nickname is Sandman and I am coding some PHP Call me
4
1917
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 my query will only check the first characters in the field to confirm whether they match the original split I did with the other code I showed under VBA breakout (you'll notice I used the code in the part I show here for part two and three of this...
2
3269
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 lines it worked quite well but srttok isnot working for multiple blank or commas. Can strtok do this kind of splitting if it cant what should i use . Unal
4
2824
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 integers later. I've bene looking around, and everything I see seems more complicated than it should be! Help!
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.