[PHP] code with comments is here

2002-04-24 Thread Kirk Babb

I have a feeling I am making this much harder than it really is!  I'm a new
programmer, and I am trying to output x number of characters per line
without splitting up the words in the string.  I want to approach as best as
possible a certain number of characters per line, let's say 80 characters,
and then cycle through the remaining parts of the array to reconstruct the
string in the same manner.

here is the code with comments:
 ?php
 $string=Why am I trying to do this crazy stuff when I should know
 better?;
//split the string into an array by spaces
$array=preg_split([\s], $string);
 // just a test of the preg_split results
 // print_r($array);
// $i is the main iteration variable, it iterates the indices of the
array
$i=0;
 $count = count($array);
// $container will hold the summed length of the array pieces
$container=0;
// start a while loop, using 20 characters as the limit for the
$container size
while ($container = 20) {
// $box holds the string length of each piece of the array
$box = strlen($array[$i]);
// $container started out at 0, now the value of $box will be added
to it
$container=$container + $box;
// increment $i and start the while again
$i++;
 }
// when the while loop condition is satisfied, start putting the pieces
of the array together
// from $array[0] to $array[$i]
 $returned_string=;
// $j is incremented in the next while loop
$j=0;
//while $j is less than or equal to the value of $i - the last piece of
the array that satisfied our
//20 character limit
while ($j = $i) {
// add each array piece to $returned_string
$returned_string .= $array[$j] .  ;
 $j++;
 }
//when this part of the string has been reconstructed, print out the
result
 print $returned_string . br;
 ?
 Like I mentioned above, this may be a case of making something much harder
than it really is.  I'm still stuck on printing the remaining parts of the
array, and I'd really like to make this a function and just give it the
string and the container length as arguments.can someone give me some
advice?  Or just tell me I'm stupid?



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] code with comments is here

2002-04-24 Thread Miguel Cruz

function wrap_lines ($str, $line_length)
{
  $r = '';
  $words = preg_split('/\s/', $str);
  $cur_pos = 0;
  foreach($words as $word)
  {
if (($cur_pos + strlen($word) + 1)  $line_length)
{
  $cur_pos = 0;
  $r .= 'br';
}
else
{   
  $r .= ' ';
  $cur_pos++;
}
$r .= $word;
$cur_pos += strlen($word);
  }
  return $r;
}

On Wed, 24 Apr 2002, Kirk Babb wrote:
 I have a feeling I am making this much harder than it really is!  I'm a new
 programmer, and I am trying to output x number of characters per line
 without splitting up the words in the string.  I want to approach as best as
 possible a certain number of characters per line, let's say 80 characters,
 and then cycle through the remaining parts of the array to reconstruct the
 string in the same manner.
 
 here is the code with comments:
  ?php
  $string=Why am I trying to do this crazy stuff when I should know
  better?;
 //split the string into an array by spaces
 $array=preg_split([\s], $string);
  // just a test of the preg_split results
  // print_r($array);
 // $i is the main iteration variable, it iterates the indices of the
 array
 $i=0;
  $count = count($array);
 // $container will hold the summed length of the array pieces
 $container=0;
 // start a while loop, using 20 characters as the limit for the
 $container size
 while ($container = 20) {
 // $box holds the string length of each piece of the array
 $box = strlen($array[$i]);
 // $container started out at 0, now the value of $box will be added
 to it
 $container=$container + $box;
 // increment $i and start the while again
 $i++;
  }
 // when the while loop condition is satisfied, start putting the pieces
 of the array together
 // from $array[0] to $array[$i]
  $returned_string=;
 // $j is incremented in the next while loop
 $j=0;
 //while $j is less than or equal to the value of $i - the last piece of
 the array that satisfied our
 //20 character limit
 while ($j = $i) {
 // add each array piece to $returned_string
 $returned_string .= $array[$j] .  ;
  $j++;
  }
 //when this part of the string has been reconstructed, print out the
 result
  print $returned_string . br;
  ?
  Like I mentioned above, this may be a case of making something much harder
 than it really is.  I'm still stuck on printing the remaining parts of the
 array, and I'd really like to make this a function and just give it the
 string and the container length as arguments.can someone give me some
 advice?  Or just tell me I'm stupid?
 
 
 
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] code with comments is here

2002-04-24 Thread Jason Wong

On Thursday 25 April 2002 00:14, Kirk Babb wrote:
 I have a feeling I am making this much harder than it really is!  I'm a new
 programmer, and I am trying to output x number of characters per line
 without splitting up the words in the string.  I want to approach as best
 as possible a certain number of characters per line, let's say 80
 characters, and then cycle through the remaining parts of the array to
 reconstruct the string in the same manner.

Have you tried the wordwrap() function?

-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *

/*
There's nothing worse for your business than extra Santa Clauses
smoking in the men's room.
-- W. Bossert
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php