Reply to Re: Splitting a string into three parts

Your name:

Reply:


Posted by gosha bine on 09/20/07 09:46

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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация