Splitting a string into three parts
Splitting a string into three parts
am 19.09.2007 21:25:47 von techusky
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?
Re: Splitting a string into three parts
am 19.09.2007 21:44:36 von Hans-Peter Sauer
<>
<1190229947.157523.58940@22g2000hsm.googlegroups.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=wordwrap($slap,200,"\n",1);
--
(c) The Amazing Krustov
Re: Splitting a string into three parts
am 19.09.2007 23:48:01 von techusky
On Sep 19, 3:44 pm, Krustov wrote:
>
> <>
>
> <1190229947.157523.58...@22g2000hsm.googlegroups.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=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.
Re: Splitting a string into three parts
am 20.09.2007 02:56:39 von Hans-Peter Sauer
<>
<1190238481.305757.111070@k79g2000hse.googlegroups.com>
> > > $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.
>
A bit luddite - but it seems to work ok .
Test
$articleBody="
Search: the web pages from the UK
New! View and manage your web history
Web Results 1 - 10 of about 82,200,000 English pages for large text
file. (0.10 seconds)
Large Text File Viewer 4.1 - Features
Have you ever felt frustrated when you justor Notepad or Word to open
it? ...
www.topshareware.com/Large-Text-Fles
I'm having to deal with some third party that I am bulk inserting ...
discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixP ost=34530 -
13k - Cached - Similar pages
Edit Large Text File
Large text file: downloadable software.
Large text file: Secure text chat, pdf to text batch converter, Secure
text chat.
www.vicman.net/lib/large/textfile - 49k - Cached - pages
Invision Power Services > Opening Large Text Files
What do you all use to open l examines a number of wayss to ...
www.4guysfromrolla.com/webtech/010401-1.shtml - 91k - Cached - Similar
pages
";
$articleBody=str_replace("\n"," ",$articleBody);
$articleBody=str_replace("\r"," ",$articleBody);
$articleBody=str_replace("\t"," ",$articleBody);
$articleBody=str_replace(" "," ",$articleBody);
$articleBody=str_replace(" "," ",$articleBody);
$bart=strlen($articleBody);
print "This string is $bart characters long .
";
$lisa=$bart/3;
$lisa=number_format($lisa,0,'.','');
$lisa=$lisa+10;
print "If 3 columns are used then its $lisa characters a column .
";
$smithers=trim($articleBody);
$newtext=wordwrap($smithers,$lisa,"\n");
$filename="zzzzz.php"; $fp=fopen($filename,"w"); fwrite ($fp,$newtext);
fwrite ($fp,"\n"); fclose($fp);
$filename="zzzzz.php"; $fp=fopen($filename,"r");
$qaz=fgets($fp); $homer[1]=trim($qaz);
$qaz=fgets($fp); $homer[2]=trim($qaz);
$qaz=fgets($fp); $homer[3]=trim($qaz);
fclose($fp);
?>
align="center">
|
|
|
|
|
Re: Splitting a string into three parts
am 20.09.2007 11:46:03 von gosha bine
On 19.09.2007 21:25 techusky@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
Re: Splitting a string into three parts
am 20.09.2007 22:04:58 von techusky
On Sep 20, 5:46 am, gosha bine wrote:
> 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.