Reply to Re: Adding line breaks to a variable dynamically

Your name:

Reply:


Posted by Rik on 12/16/06 00:56

howarthc@gmail.com wrote:
> OK - here is what I want to do - but I am lost how to do it.
>
> I have a variable
>
> $mystring = "one two three four five six seven eight nine"
>
> This variable $mystring can be 4 words long or it could be 50 words
> long it is totally variable. What I want to do is take the number of
> words in $mystring, divide by three and insert a <br> at the end of
> each "line".
>
> Example: $mystring = "one two three four five six seven eight nine"
>
> Convert to:
> one two three<br>
> four five six<br>
> seven eight nine<br>
>
> Example: $mystring = "one two three four five six seven eight nine ten
> eleven twelve"
>
> Convert to:
> one two three four<br>
> five six seven eight<br>
> nine ten eleven twelve<br>
>
> I know I can use explode or strip with a field seperator of " " to
> break the variable into component parts - but my non-programmer brain
> is confused how I would do this.

Unfortunately, divided by three is not always an integer....
I've taken the liberty to assume "extra's on last row":

$array = split(' ',$string);
$newstring = '';
$rows = 3;
while($rows >= 1){
$newstring .= implode('
',array_splice($array,0,floor(count($array)/$rows--))).'<br>';
}
echo $newstring;
--
Rik Wasmus

[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

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