Re: Array Problem
From: jn (jsumner1_at_cfl.rr.com)
Date: 11/08/03
- Next message: ShipiboConibo: "Re: fopen doesn't work in any sort of loop"
- Previous message: <>: "Array Problem"
- In reply to: <>: "Array Problem"
- Next in thread: <>: "Re: Array Problem"
- Reply: <>: "Re: Array Problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: ShipiboConibo: "Re: fopen doesn't work in any sort of loop"
- Previous message: <>: "Array Problem"
- In reply to: <>: "Array Problem"
- Next in thread: <>: "Re: Array Problem"
- Reply: <>: "Re: Array Problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|