Re: Array Problem

From: jn (jsumner1_at_cfl.rr.com)
Date: 11/08/03


Date: Sat, 08 Nov 2003 17:35:11 GMT


"<>" <loopback@44.255.255.255> wrote in message
news:9H9rb.124759$ZH4.91655@twister.socal.rr.com...
> I thought I understood how to traverse an array, but I guess I was wrong.
I
> have tried writing the code snippet below as: while, for, foreach... and
get
> the same (consistently wrong) result each time!
>
> $data is a string variable containing lines of text. Each line is
terminated
> with a break (<br>). I need to analyze each line to ensure it does not
exceed
> $maxLength. If it is <= $maxLength do nothing, otherwise truncate the line
at
> $maxLength minus 3 and add ellipses (...). I then need to reassemble
> everything back into a string variable, for later printing. Here is my
code:
>
> $line = explode('<br>', $data);
> $count = count($line);
> $i=1;
> while ($i <= $count) {
> $line = (strlen($line)>$maxLength) ?
(substr($line,0,($maxLength-3)).'...')
> : $line;
> $i++;
> }
> $data = implode('<br>', $line);
> echo $data;
>
> I know I am forming the array correctly, because $line and $count display
as
> expected. I know that the ternary is correct, because it produces the
desired
> result using the original string (i.e., substituting $data outside the
> 'while' loop).
>
> Any help will be greatly appreciated.
>

You aren't using your counter for anything. You are trying to run this on
every element, not the whole array at once. You should do this:

$line[$i] = (strlen($line[$i])>$maxLength) ?
(substr($line[$i],0,($maxLength-3)).'...') : $line[$i];

HTH



Relevant Pages

  • Re: Array Problem
    ... > I thought I understood how to traverse an array, but I guess I was wrong. ... > have tried writing the code snippet below as: ...
    (comp.lang.php)
  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: Problem with a script
    ... Okay, so variables have unique labels, that doesn't mean they still couldn't be handled in a loop. ... You still have them as uniquely named array indexes... ... I believe that for the new guy this code would be readable, and identifying problems should really not be any more difficult with this, plus I think that it actually might save some time to write the actual code from the beginnig, even though it's not at it's final stage, instead of first writing everything spread out, and then rewriting the same code again cleaned. ... If you expect a person to spend an hour reading your entire code and parsing it in their head, you wont get any help and have to solve the problem by yourself. ...
    (comp.lang.php)
  • Re: problem reading/writing structures from and to files
    ... My progam can write the Data array into a binary file, after writing ... FILE *somefile; ... although the initial fwrite() -- writing bytes ...
    (comp.lang.c)
  • Re: Read and re-write file with one open?
    ... This opens a file for reading and writing, reads the file into an array, ... altered data back to the file, and the file contains half altered data ...
    (comp.lang.ruby)